First go at reading directories on public SMB shares.
[wine/multimedia.git] / files / smb.c
blob16825c14503a4aa66c6541dcf6f1d8eec6a267ff
1 /*
2 * Copyright (C) 2002 Mike McCormack
4 * CIFS implementation for WINE
6 * This is a WINE's implementation of the Common Internet File System
8 * for specification see:
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
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.
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.
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
31 * FIXME:
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.
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.
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.
47 * - We don't deal with SMB dialects at all.
49 * - SMB supports passing unicode over the wire, should use this if possible.
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.
56 #include "config.h"
57 #include "wine/port.h"
59 #include <assert.h>
60 #include <ctype.h>
61 #include <errno.h>
62 #include <fcntl.h>
63 #include <stdlib.h>
64 #include <stdio.h>
65 #include <string.h>
66 #ifdef HAVE_SYS_ERRNO_H
67 #include <sys/errno.h>
68 #endif
69 #include <sys/types.h>
70 #include <sys/stat.h>
71 #ifdef HAVE_SYS_MMAN_H
72 #include <sys/mman.h>
73 #endif
74 #include <sys/time.h>
75 #include <sys/poll.h>
76 #include <time.h>
77 #include <unistd.h>
78 #include <utime.h>
79 #ifdef HAVE_SYS_SOCKET_H
80 # include <sys/socket.h>
81 #endif
82 #include <sys/types.h>
83 #ifdef HAVE_NETINET_IN_SYSTM_H
84 #include <netinet/in_systm.h>
85 #endif
86 #ifdef HAVE_NETINET_IN_H
87 #include <netinet/in.h>
88 #endif
89 #ifdef HAVE_NETINET_IP_H
90 #include <netinet/ip.h>
91 #endif
92 #ifdef HAVE_ARPA_INET_H
93 #include <arpa/inet.h>
94 #endif
95 #ifdef HAVE_NETDB_H
96 #include <netdb.h>
97 #endif
99 #include "winerror.h"
100 #include "windef.h"
101 #include "winbase.h"
102 #include "file.h"
103 #include "heap.h"
105 #include "smb.h"
107 #include "wine/server.h"
108 #include "wine/debug.h"
110 WINE_DEFAULT_DEBUG_CHANNEL(file);
112 #define MAX_HOST_NAME 15
113 #define NB_TIMEOUT 10000
115 USHORT SMB_MultiplexId = 0;
117 struct NB_Buffer
119 unsigned char *buffer;
120 int len;
123 static int netbios_name(const char *p, unsigned char *buffer)
125 char ch;
126 int i,len=0;
128 buffer[len++]=' ';
129 for(i=0; i<=MAX_HOST_NAME; i++)
131 if(i<MAX_HOST_NAME)
133 if(*p)
134 ch = *p++&0xdf; /* add character from hostname */
135 else
136 ch = ' '; /* add padding */
138 else
139 ch = 0; /* add terminator */
140 buffer[len++] = ((ch&0xf0) >> 4) + 'A';
141 buffer[len++] = (ch&0x0f) + 'A';
143 buffer[len++] = 0; /* add second terminator */
144 return len;
147 static DWORD NB_NameReq(LPCSTR host, unsigned char *buffer, int len)
149 int trn = 1234,i=0;
151 NBR_ADDWORD(&buffer[i],trn); i+=2;
152 NBR_ADDWORD(&buffer[i],0x0110); i+=2;
153 NBR_ADDWORD(&buffer[i],0x0001); i+=2;
154 NBR_ADDWORD(&buffer[i],0x0000); i+=2;
155 NBR_ADDWORD(&buffer[i],0x0000); i+=2;
156 NBR_ADDWORD(&buffer[i],0x0000); i+=2;
158 i += netbios_name(host,&buffer[i]);
160 NBR_ADDWORD(&buffer[i],0x0020); i+=2;
161 NBR_ADDWORD(&buffer[i],0x0001); i+=2;
163 TRACE("packet is %d bytes in length\n",i);
166 int j;
167 for(j=0; j<i; j++)
168 printf("%02x%c",buffer[j],(((j+1)%16)&&((j+1)!=j))?' ':'\n');
171 return i;
174 /* unc = \\hostname\share\file... */
175 static BOOL UNC_SplitName(LPSTR unc, LPSTR *hostname, LPSTR *share, LPSTR *file)
177 char *p;
179 TRACE("%s\n",unc);
181 p = strchr(unc,'\\');
182 if(!p)
183 return FALSE;
184 p = strchr(p+1,'\\');
185 if(!p)
186 return FALSE;
187 *hostname=++p;
189 p = strchr(p,'\\');
190 if(!p)
191 return FALSE;
192 *p=0;
193 *share = ++p;
195 p = strchr(p,'\\');
196 if(!p)
197 return FALSE;
198 *p=0;
199 *file = ++p;
201 return TRUE;
204 static BOOL NB_Lookup(LPCSTR host, struct sockaddr_in *addr)
206 int fd,on=1,r,len,i,fromsize;
207 struct pollfd fds;
208 struct sockaddr_in sin,fromaddr;
209 unsigned char buffer[256];
211 fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
212 if(fd<0)
213 return FALSE;
215 r = setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &on, sizeof on);
216 if(r<0)
217 goto err;
219 if(0==inet_aton("255.255.255.255", (struct in_addr *)&sin.sin_addr.s_addr))
221 FIXME("Error getting bcast address\n");
222 goto err;
224 sin.sin_family = AF_INET;
225 sin.sin_port = htons(137);
227 len = NB_NameReq(host,buffer,sizeof buffer);
228 if(len<=0)
229 goto err;
231 r = sendto(fd, buffer, len, 0, &sin, sizeof sin);
232 if(r<0)
234 FIXME("Error sending packet\n");
235 goto err;
238 fds.fd = fd;
239 fds.events = POLLIN;
240 fds.revents = 0;
242 /* FIXME: this is simple and easily fooled logic
243 * we should loop until we receive the correct packet or timeout
245 r = poll(&fds,1,NB_TIMEOUT);
246 if(r!=1)
247 goto err;
249 TRACE("Got response!\n");
251 fromsize = sizeof (fromaddr);
252 r = recvfrom(fd, buffer, sizeof buffer, 0, &fromaddr, &fromsize);
253 if(r<0)
254 goto err;
256 TRACE("%d bytes received\n",r);
258 if(r!=62)
259 goto err;
261 for(i=0; i<r; i++)
262 DPRINTF("%02X%c",buffer[i],(((i+1)!=r)&&((i+1)%16))?' ':'\n');
263 DPRINTF("\n");
265 if(0x0f & buffer[3])
266 goto err;
268 TRACE("packet is OK\n");
270 memcpy(&addr->sin_addr, &buffer[58], sizeof addr->sin_addr);
272 close(fd);
273 return TRUE;
275 err:
276 close(fd);
277 return FALSE;
280 #define NB_FIRST 0x40
282 #define NB_HDRSIZE 4
284 #define NB_SESSION_MSG 0x00
285 #define NB_SESSION_REQ 0x81
287 /* RFC 1002, section 4.3.2 */
288 static BOOL NB_SessionReq(int fd, char *called, char *calling)
290 unsigned char buffer[0x100];
291 int len = 0,r;
292 struct pollfd fds;
294 TRACE("called %s, calling %s\n",called,calling);
296 buffer[0] = NB_SESSION_REQ;
297 buffer[1] = NB_FIRST;
299 netbios_name(called, &buffer[NB_HDRSIZE]);
300 len += 34;
301 netbios_name(calling, &buffer[NB_HDRSIZE+len]);
302 len += 34;
304 NBR_ADDWORD(&buffer[2],len);
306 /* for(i=0; i<(len+NB_HDRSIZE); i++)
307 DPRINTF("%02X%c",buffer[i],(((i+1)!=(len+4))&&((i+1)%16))?' ':'\n'); */
309 r = write(fd,buffer,len+4);
310 if(r<0)
312 ERR("Write failed\n");
313 return FALSE;
316 fds.fd = fd;
317 fds.events = POLLIN;
318 fds.revents = 0;
320 r = poll(&fds,1,NB_TIMEOUT);
321 if(r!=1)
323 ERR("Poll failed\n");
324 return FALSE;
327 r = read(fd, buffer, NB_HDRSIZE);
328 if((r!=NB_HDRSIZE) || (buffer[0]!=0x82))
330 TRACE("Received %d bytes\n",r);
331 TRACE("%02x %02x %02x %02x\n", buffer[0],buffer[1],buffer[2],buffer[3]);
332 return FALSE;
335 return TRUE;
338 static BOOL NB_SendData(int fd, struct NB_Buffer *out)
340 unsigned char buffer[NB_HDRSIZE];
341 int r;
343 /* CHECK: is it always OK to do this in two writes? */
344 /* perhaps use scatter gather sendmsg instead? */
346 buffer[0] = NB_SESSION_MSG;
347 buffer[1] = NB_FIRST;
348 NBR_ADDWORD(&buffer[2],out->len);
350 r = write(fd, buffer, NB_HDRSIZE);
351 if(r!=NB_HDRSIZE)
352 return FALSE;
354 r = write(fd, out->buffer, out->len);
355 if(r!=out->len)
357 ERR("write failed\n");
358 return FALSE;
361 return TRUE;
364 static BOOL NB_RecvData(int fd, struct NB_Buffer *rx)
366 int r;
367 unsigned char buffer[NB_HDRSIZE];
369 r = read(fd, buffer, NB_HDRSIZE);
370 if((r!=NB_HDRSIZE) || (buffer[0]!=NB_SESSION_MSG))
372 ERR("Received %d bytes\n",r);
373 return FALSE;
376 rx->len = NBR_GETWORD(&buffer[2]);
378 rx->buffer = HeapAlloc(GetProcessHeap(), 0, rx->len);
379 if(!rx->buffer)
380 return FALSE;
382 r = read(fd, rx->buffer, rx->len);
383 if(rx->len!=r)
385 TRACE("Received %d bytes\n",r);
386 HeapFree(GetProcessHeap(), 0, rx->buffer);
387 rx->buffer = 0;
388 rx->len = 0;
389 return FALSE;
392 return TRUE;
395 static BOOL NB_Transaction(int fd, struct NB_Buffer *in, struct NB_Buffer *out)
397 int r;
398 struct pollfd fds;
400 if(TRACE_ON(file))
402 int i;
403 DPRINTF("Sending request:\n");
404 for(i=0; i<in->len; i++)
405 DPRINTF("%02X%c",in->buffer[i],(((i+1)!=in->len)&&((i+1)%16))?' ':'\n');
408 if(!NB_SendData(fd,in))
409 return FALSE;
411 fds.fd = fd;
412 fds.events = POLLIN;
413 fds.revents = 0;
415 r = poll(&fds,1,NB_TIMEOUT);
416 if(r!=1)
418 ERR("Poll failed\n");
419 return FALSE;
422 if(!NB_RecvData(fd, out))
423 return FALSE;
425 if(TRACE_ON(file))
427 int i;
428 DPRINTF("Got response:\n");
429 for(i=0; i<out->len; i++)
430 DPRINTF("%02X%c",out->buffer[i],(((i+1)!=out->len)&&((i+1)%16))?' ':'\n');
433 return TRUE;
436 #define SMB_ADDHEADER(b,l) { b[(l)++]=0xff; b[(l)++]='S'; b[(l)++]='M'; b[(l)++]='B'; }
437 #define SMB_ADDERRINFO(b,l) { b[(l)++]=0; b[(l)++]=0; b[(l)++]=0; b[(l)++]=0; }
438 #define SMB_ADDPADSIG(b,l) { memset(&b[l],0,12); l+=12; }
440 #define SMB_ERRCLASS 5
441 #define SMB_ERRCODE 7
442 #define SMB_TREEID 24
443 #define SMB_PROCID 26
444 #define SMB_USERID 28
445 #define SMB_PLEXID 30
446 #define SMB_PCOUNT 32
447 #define SMB_HDRSIZE 33
449 static DWORD SMB_GetError(unsigned char *buffer)
451 char *err_class;
453 switch(buffer[SMB_ERRCLASS])
455 case 0:
456 return STATUS_SUCCESS;
457 case 1:
458 err_class = "DOS";
459 break;
460 case 2:
461 err_class = "net server";
462 break;
463 case 3:
464 err_class = "hardware";
465 break;
466 case 0xff:
467 err_class = "smb";
468 break;
469 default:
470 err_class = "unknown";
471 break;
474 ERR("%s error %d \n",err_class, buffer[SMB_ERRCODE]);
476 /* FIXME: return propper error codes */
477 return STATUS_INVALID_PARAMETER;
480 static int SMB_Header(unsigned char *buffer, unsigned char command, USHORT tree_id, USHORT user_id)
482 int len = 0;
483 DWORD id;
485 /* 0 */
486 SMB_ADDHEADER(buffer,len);
488 /* 4 */
489 buffer[len++] = command;
491 /* 5 */
492 SMB_ADDERRINFO(buffer,len)
494 /* 9 */
495 buffer[len++] = 0x00; /* flags */
496 SMB_ADDWORD(&buffer[len],1); len += 2; /* flags2 */
498 /* 12 */
499 SMB_ADDPADSIG(buffer,len)
501 /* 24 */
502 SMB_ADDWORD(&buffer[len],tree_id); len += 2; /* treeid */
503 id = GetCurrentThreadId();
504 SMB_ADDWORD(&buffer[len],id); len += 2; /* process id */
505 SMB_ADDWORD(&buffer[len],user_id); len += 2; /* user id */
506 SMB_ADDWORD(&buffer[len],SMB_MultiplexId); len += 2; /* multiplex id */
507 SMB_MultiplexId++;
509 return len;
512 static const char *SMB_ProtocolDialect = "NT LM 0.12";
513 /* = "Windows for Workgroups 3.1a"; */
515 /* FIXME: support multiple SMB dialects */
516 static BOOL SMB_NegotiateProtocol(int fd, USHORT *dialect)
518 unsigned char buf[0x100];
519 int buflen = 0;
520 struct NB_Buffer tx, rx;
522 TRACE("\n");
524 memset(buf,0,sizeof buf);
526 tx.buffer = buf;
527 tx.len = SMB_Header(tx.buffer, SMB_COM_NEGOTIATE, 0, 0);
529 /* parameters */
530 tx.buffer[tx.len++] = 0; /* no parameters */
532 /* command buffer */
533 buflen = strlen(SMB_ProtocolDialect)+2; /* include type and nul byte */
534 SMB_ADDWORD(&tx.buffer[tx.len],buflen); tx.len += 2;
536 tx.buffer[tx.len] = 0x02;
537 strcpy(&tx.buffer[tx.len+1],SMB_ProtocolDialect);
538 tx.len += buflen;
540 rx.buffer = NULL;
541 rx.len = 0;
542 if(!NB_Transaction(fd, &tx, &rx))
544 ERR("Failed\n");
545 return FALSE;
548 if(!rx.buffer)
549 return FALSE;
551 /* FIXME: check response */
552 if(SMB_GetError(rx.buffer))
554 ERR("returned error\n");
555 HeapFree(GetProcessHeap(),0,rx.buffer);
556 return FALSE;
559 HeapFree(GetProcessHeap(),0,rx.buffer);
561 *dialect = 0;
563 return TRUE;
566 #define SMB_PARAM_COUNT(buffer) ((buffer)[SMB_PCOUNT])
567 #define SMB_PARAM(buffer,n) SMB_GETWORD(&(buffer)[SMB_HDRSIZE+2*(n)])
568 #define SMB_BUFFER_COUNT(buffer) SMB_GETWORD(buffer+SMB_HDRSIZE+2*SMB_PARAM_COUNT(buffer))
569 #define SMB_BUFFER(buffer,n) ((buffer)[SMB_HDRSIZE + 2*SMB_PARAM_COUNT(buffer) + 2 + (n) ])
571 static BOOL SMB_SessionSetup(int fd, USHORT *userid)
573 unsigned char buf[0x100];
574 int pcount,bcount;
575 struct NB_Buffer rx, tx;
577 memset(buf,0,sizeof buf);
578 tx.buffer = buf;
580 tx.len = SMB_Header(tx.buffer, SMB_COM_SESSION_SETUP_ANDX, 0, 0);
582 tx.buffer[tx.len++] = 0; /* no parameters? */
584 tx.buffer[tx.len++] = 0xff; /* AndXCommand: secondary request */
585 tx.buffer[tx.len++] = 0x00; /* AndXReserved */
586 SMB_ADDWORD(&tx.buffer[tx.len],0); /* AndXOffset */
587 tx.len += 2;
588 SMB_ADDWORD(&tx.buffer[tx.len],0x400); /* MaxBufferSize */
589 tx.len += 2;
590 SMB_ADDWORD(&tx.buffer[tx.len],1); /* MaxMpxCount */
591 tx.len += 2;
592 SMB_ADDWORD(&tx.buffer[tx.len],0); /* VcNumber */
593 tx.len += 2;
594 SMB_ADDWORD(&tx.buffer[tx.len],0); /* SessionKey */
595 tx.len += 2;
596 SMB_ADDWORD(&tx.buffer[tx.len],0); /* SessionKey */
597 tx.len += 2;
598 SMB_ADDWORD(&tx.buffer[tx.len],0); /* Password length */
599 tx.len += 2;
600 SMB_ADDWORD(&tx.buffer[tx.len],0); /* Reserved */
601 tx.len += 2;
602 SMB_ADDWORD(&tx.buffer[tx.len],0); /* Reserved */
603 tx.len += 2;
605 /* FIXME: add name and password here */
606 tx.buffer[tx.len++] = 0; /* number of bytes in password */
608 rx.buffer = NULL;
609 rx.len = 0;
610 if(!NB_Transaction(fd, &tx, &rx))
611 return FALSE;
613 if(!rx.buffer)
614 return FALSE;
616 if(SMB_GetError(rx.buffer))
617 goto done;
619 pcount = SMB_PARAM_COUNT(rx.buffer);
621 if( (SMB_HDRSIZE+pcount*2) > rx.len )
623 ERR("Bad parameter count %d\n",pcount);
624 goto done;
627 if(TRACE_ON(file))
629 int i;
630 DPRINTF("SMB_COM_SESSION_SETUP response, %d args: ",pcount);
631 for(i=0; i<pcount; i++)
632 DPRINTF("%04x ",SMB_PARAM(rx.buffer,i));
633 DPRINTF("\n");
636 bcount = SMB_BUFFER_COUNT(rx.buffer);
637 if( (SMB_HDRSIZE+pcount*2+2+bcount) > rx.len )
639 ERR("parameter count %x, buffer count %x, len %x\n",pcount,bcount,rx.len);
640 goto done;
643 if(TRACE_ON(file))
645 int i;
646 DPRINTF("response buffer %d bytes: ",bcount);
647 for(i=0; i<bcount; i++)
649 unsigned char ch = SMB_BUFFER(rx.buffer,i);
650 DPRINTF("%c", isprint(ch)?ch:' ');
652 DPRINTF("\n");
655 *userid = SMB_GETWORD(&rx.buffer[SMB_USERID]);
657 HeapFree(GetProcessHeap(),0,rx.buffer);
658 return TRUE;
660 done:
661 HeapFree(GetProcessHeap(),0,rx.buffer);
662 return FALSE;
666 static BOOL SMB_TreeConnect(int fd, USHORT user_id, LPCSTR share_name, USHORT *treeid)
668 unsigned char buf[0x100];
669 int slen;
670 struct NB_Buffer rx,tx;
672 TRACE("%s\n",share_name);
674 memset(buf,0,sizeof buf);
675 tx.buffer = buf;
677 tx.len = SMB_Header(tx.buffer, SMB_COM_TREE_CONNECT, 0, user_id);
679 tx.buffer[tx.len++] = 4; /* parameters */
681 tx.buffer[tx.len++] = 0xff; /* AndXCommand: secondary request */
682 tx.buffer[tx.len++] = 0x00; /* AndXReserved */
683 SMB_ADDWORD(&tx.buffer[tx.len],0); /* AndXOffset */
684 tx.len += 2;
685 SMB_ADDWORD(&tx.buffer[tx.len],0); /* Flags */
686 tx.len += 2;
687 SMB_ADDWORD(&tx.buffer[tx.len],1); /* Password length */
688 tx.len += 2;
690 /* SMB command buffer */
691 SMB_ADDWORD(&tx.buffer[tx.len],3); /* command buffer len */
692 tx.len += 2;
693 tx.buffer[tx.len++] = 0; /* null terminated password */
695 slen = strlen(share_name);
696 if(slen<(sizeof buf-tx.len))
697 strcpy(&tx.buffer[tx.len], share_name);
698 else
699 return FALSE;
700 tx.len += slen+1;
702 /* name of the service */
703 tx.buffer[tx.len++] = 0;
705 rx.buffer = NULL;
706 rx.len = 0;
707 if(!NB_Transaction(fd, &tx, &rx))
708 return FALSE;
710 if(!rx.buffer)
711 return FALSE;
713 if(SMB_GetError(rx.buffer))
715 HeapFree(GetProcessHeap(),0,rx.buffer);
716 return FALSE;
719 *treeid = SMB_GETWORD(&rx.buffer[SMB_TREEID]);
721 HeapFree(GetProcessHeap(),0,rx.buffer);
722 TRACE("OK, treeid = %04x\n", *treeid);
724 return TRUE;
727 #if 0 /* not yet */
728 static BOOL SMB_NtCreateOpen(int fd, USHORT tree_id, USHORT user_id, USHORT dialect,
729 LPCSTR filename, DWORD access, DWORD sharing,
730 LPSECURITY_ATTRIBUTES sa, DWORD creation,
731 DWORD attributes, HANDLE template, USHORT *file_id )
733 unsigned char buffer[0x100];
734 int len = 0,slen;
736 TRACE("%s\n",filename);
738 memset(buffer,0,sizeof buffer);
740 len = SMB_Header(buffer, SMB_COM_NT_CREATE_ANDX, tree_id, user_id);
742 /* 0 */
743 buffer[len++] = 24; /* parameters */
745 buffer[len++] = 0xff; /* AndXCommand: secondary request */
746 buffer[len++] = 0x00; /* AndXReserved */
747 SMB_ADDWORD(&buffer[len],0); len += 2; /* AndXOffset */
749 buffer[len++] = 0; /* reserved */
750 slen = strlen(filename);
751 SMB_ADDWORD(&buffer[len],slen); len += 2; /* name length */
753 /* 0x08 */
754 SMB_ADDDWORD(&buffer[len],0); len += 4; /* flags */
755 SMB_ADDDWORD(&buffer[len],0); len += 4; /* root directory fid */
756 /* 0x10 */
757 SMB_ADDDWORD(&buffer[len],access); len += 4; /* access */
758 SMB_ADDDWORD(&buffer[len],0); len += 4; /* allocation size */
759 /* 0x18 */
760 SMB_ADDDWORD(&buffer[len],0); len += 4; /* root directory fid */
762 /* 0x1c */
763 SMB_ADDDWORD(&buffer[len],0); len += 4; /* initial allocation */
764 SMB_ADDDWORD(&buffer[len],0); len += 4;
766 /* 0x24 */
767 SMB_ADDDWORD(&buffer[len],attributes); len += 4; /* ExtFileAttributes*/
769 /* 0x28 */
770 SMB_ADDDWORD(&buffer[len],sharing); len += 4; /* ShareAccess */
772 /* 0x2c */
773 TRACE("creation = %08lx\n",creation);
774 SMB_ADDDWORD(&buffer[len],creation); len += 4; /* CreateDisposition */
776 /* 0x30 */
777 SMB_ADDDWORD(&buffer[len],creation); len += 4; /* CreateOptions */
779 /* 0x34 */
780 SMB_ADDDWORD(&buffer[len],0); len += 4; /* Impersonation */
782 /* 0x38 */
783 buffer[len++] = 0; /* security flags */
785 /* 0x39 */
786 SMB_ADDWORD(&buffer[len],slen); len += 2; /* size of buffer */
788 if(slen<(sizeof buffer-len))
789 strcpy(&buffer[len], filename);
790 else
791 return FALSE;
792 len += slen+1;
794 /* name of the file */
795 buffer[len++] = 0;
797 if(!NB_Transaction(fd, buffer, len, &len))
798 return FALSE;
800 if(SMB_GetError(buffer))
801 return FALSE;
803 TRACE("OK\n");
805 /* FIXME */
806 /* *file_id = SMB_GETWORD(&buffer[xxx]); */
807 *file_id = 0;
808 return FALSE;
810 return TRUE;
812 #endif
814 static USHORT SMB_GetMode(DWORD access, DWORD sharing)
816 USHORT mode=0;
818 switch(access&(GENERIC_READ|GENERIC_WRITE))
820 case GENERIC_READ:
821 mode |= OF_READ;
822 break;
823 case GENERIC_WRITE:
824 mode |= OF_WRITE;
825 break;
826 case (GENERIC_READ|GENERIC_WRITE):
827 mode |= OF_READWRITE;
828 break;
831 switch(sharing&(FILE_SHARE_READ|FILE_SHARE_WRITE))
833 case (FILE_SHARE_READ|FILE_SHARE_WRITE):
834 mode |= OF_SHARE_DENY_NONE;
835 break;
836 case FILE_SHARE_READ:
837 mode |= OF_SHARE_DENY_WRITE;
838 break;
839 case FILE_SHARE_WRITE:
840 mode |= OF_SHARE_DENY_READ;
841 break;
842 default:
843 mode |= OF_SHARE_EXCLUSIVE;
844 break;
847 return mode;
850 #if 0 /* not yet */
851 /* inverse of FILE_ConvertOFMode */
852 static BOOL SMB_OpenAndX(int fd, USHORT tree_id, USHORT user_id, USHORT dialect,
853 LPCSTR filename, DWORD access, DWORD sharing,
854 DWORD creation, DWORD attributes, USHORT *file_id )
856 unsigned char buffer[0x100];
857 int len = 0;
858 USHORT mode;
860 TRACE("%s\n",filename);
862 mode = SMB_GetMode(access,sharing);
864 memset(buffer,0,sizeof buffer);
866 len = SMB_Header(buffer, SMB_COM_OPEN_ANDX, tree_id, user_id);
868 /* 0 */
869 buffer[len++] = 15; /* parameters */
870 buffer[len++] = 0xff; /* AndXCommand: secondary request */
871 buffer[len++] = 0x00; /* AndXReserved */
872 SMB_ADDWORD(buffer+len,0); len+=2; /* AndXOffset */
873 SMB_ADDWORD(buffer+len,0); len+=2; /* Flags */
874 SMB_ADDWORD(buffer+len,mode); len+=2; /* desired access */
875 SMB_ADDWORD(buffer+len,0); len+=2; /* search attributes */
876 SMB_ADDWORD(buffer+len,0); len+=2;
878 /*FIXME: complete */
879 return FALSE;
881 #endif
884 static BOOL SMB_Open(int fd, USHORT tree_id, USHORT user_id, USHORT dialect,
885 LPCSTR filename, DWORD access, DWORD sharing,
886 DWORD creation, DWORD attributes, USHORT *file_id )
888 unsigned char buf[0x100];
889 int slen,pcount,i;
890 USHORT mode = SMB_GetMode(access,sharing);
891 struct NB_Buffer rx,tx;
893 TRACE("%s\n",filename);
895 memset(buf,0,sizeof buf);
897 tx.buffer = buf;
898 tx.len = SMB_Header(tx.buffer, SMB_COM_OPEN, tree_id, user_id);
900 /* 0 */
901 tx.buffer[tx.len++] = 2; /* parameters */
902 SMB_ADDWORD(tx.buffer+tx.len,mode); tx.len+=2;
903 SMB_ADDWORD(tx.buffer+tx.len,0); tx.len+=2; /* search attributes */
905 slen = strlen(filename)+2; /* inc. nul and BufferFormat */
906 SMB_ADDWORD(tx.buffer+tx.len,slen); tx.len+=2;
908 tx.buffer[tx.len] = 0x04; /* BufferFormat */
909 strcpy(&tx.buffer[tx.len+1],filename);
910 tx.len += slen;
912 rx.buffer = NULL;
913 rx.len = 0;
914 if(!NB_Transaction(fd, &tx, &rx))
915 return FALSE;
917 if(!rx.buffer)
918 return FALSE;
920 if(SMB_GetError(rx.buffer))
921 return FALSE;
923 pcount = SMB_PARAM_COUNT(rx.buffer);
925 if( (SMB_HDRSIZE+pcount*2) > rx.len )
927 ERR("Bad parameter count %d\n",pcount);
928 return FALSE;
931 TRACE("response, %d args: ",pcount);
932 for(i=0; i<pcount; i++)
933 DPRINTF("%04x ",SMB_PARAM(rx.buffer,i));
934 DPRINTF("\n");
936 *file_id = SMB_PARAM(rx.buffer,0);
938 TRACE("file_id = %04x\n",*file_id);
940 return TRUE;
944 static BOOL SMB_Read(int fd, USHORT tree_id, USHORT user_id, USHORT dialect,
945 USHORT file_id, DWORD offset, LPVOID out, USHORT count, LPUSHORT read)
947 int buf_size,n,i;
948 struct NB_Buffer rx,tx;
950 TRACE("user %04x tree %04x file %04x count %04x offset %08lx\n",
951 user_id, tree_id, file_id, count, offset);
953 buf_size = count+0x100;
954 tx.buffer = (unsigned char *) HeapAlloc(GetProcessHeap(),0,buf_size);
956 memset(tx.buffer,0,buf_size);
958 tx.len = SMB_Header(tx.buffer, SMB_COM_READ, tree_id, user_id);
960 tx.buffer[tx.len++] = 5;
961 SMB_ADDWORD(&tx.buffer[tx.len],file_id); tx.len += 2;
962 SMB_ADDWORD(&tx.buffer[tx.len],count); tx.len += 2;
963 SMB_ADDDWORD(&tx.buffer[tx.len],offset); tx.len += 4;
964 SMB_ADDWORD(&tx.buffer[tx.len],0); tx.len += 2; /* how many more bytes will be read */
966 tx.buffer[tx.len++] = 0;
968 rx.buffer = NULL;
969 rx.len = 0;
970 if(!NB_Transaction(fd, &tx, &rx))
972 HeapFree(GetProcessHeap(),0,tx.buffer);
973 return FALSE;
976 if(SMB_GetError(rx.buffer))
978 HeapFree(GetProcessHeap(),0,rx.buffer);
979 HeapFree(GetProcessHeap(),0,tx.buffer);
980 return FALSE;
983 n = SMB_PARAM_COUNT(rx.buffer);
985 if( (SMB_HDRSIZE+n*2) > rx.len )
987 HeapFree(GetProcessHeap(),0,rx.buffer);
988 HeapFree(GetProcessHeap(),0,tx.buffer);
989 ERR("Bad parameter count %d\n",n);
990 return FALSE;
993 TRACE("response, %d args: ",n);
994 for(i=0; i<n; i++)
995 DPRINTF("%04x ",SMB_PARAM(rx.buffer,i));
996 DPRINTF("\n");
998 n = SMB_PARAM(rx.buffer,5) - 3;
999 if(n>count)
1000 n=count;
1002 memcpy( out, &SMB_BUFFER(rx.buffer,3), n);
1004 TRACE("Read %d bytes\n",n);
1005 *read = n;
1007 HeapFree(GetProcessHeap(),0,tx.buffer);
1008 HeapFree(GetProcessHeap(),0,rx.buffer);
1010 return TRUE;
1015 * setup_count : number of USHORTs in the setup string
1017 struct SMB_Trans2Info
1019 struct NB_Buffer buf;
1020 unsigned char *setup;
1021 int setup_count;
1022 unsigned char *params;
1023 int param_count;
1024 unsigned char *data;
1025 int data_count;
1029 * Do an SMB transaction
1031 * This function allocates memory in the recv structure. It is
1032 * the caller's responsibility to free the memory if it finds
1033 * that recv->buf.buffer is nonzero.
1035 static BOOL SMB_Transaction2(int fd, int tree_id, int user_id,
1036 struct SMB_Trans2Info *send,
1037 struct SMB_Trans2Info *recv)
1039 int buf_size;
1040 const int retmaxparams = 0xf000;
1041 const int retmaxdata = 1024;
1042 const int retmaxsetup = 0; /* FIXME */
1043 const int flags = 0;
1044 const int timeout = 0;
1045 int param_ofs, data_ofs;
1046 struct NB_Buffer tx;
1047 BOOL ret = FALSE;
1049 buf_size = 0x100 + send->setup_count*2 + send->param_count + send->data_count ;
1050 tx.buffer = (unsigned char *) HeapAlloc(GetProcessHeap(),0,buf_size);
1052 tx.len = SMB_Header(tx.buffer, SMB_COM_TRANSACTION2, tree_id, user_id);
1054 tx.buffer[tx.len++] = 14 + send->setup_count;
1055 SMB_ADDWORD(&tx.buffer[tx.len],send->param_count); /* total param bytes sent */
1056 tx.len += 2;
1057 SMB_ADDWORD(&tx.buffer[tx.len],send->data_count); /* total data bytes sent */
1058 tx.len += 2;
1059 SMB_ADDWORD(&tx.buffer[tx.len],retmaxparams); /*max parameter bytes to return */
1060 tx.len += 2;
1061 SMB_ADDWORD(&tx.buffer[tx.len],retmaxdata); /* max data bytes to return */
1062 tx.len += 2;
1063 tx.buffer[tx.len++] = retmaxsetup;
1064 tx.buffer[tx.len++] = 0; /* reserved1 */
1066 SMB_ADDWORD(&tx.buffer[tx.len],flags); /* flags */
1067 tx.len += 2;
1068 SMB_ADDDWORD(&tx.buffer[tx.len],timeout); /* timeout */
1069 tx.len += 4;
1070 SMB_ADDWORD(&tx.buffer[tx.len],0); /* reserved2 */
1071 tx.len += 2;
1072 SMB_ADDWORD(&tx.buffer[tx.len],send->param_count); /* parameter count - this buffer */
1073 tx.len += 2;
1075 param_ofs = tx.len; /* parameter offset */
1076 tx.len += 2;
1077 SMB_ADDWORD(&tx.buffer[tx.len],send->data_count); /* data count */
1078 tx.len += 2;
1080 data_ofs = tx.len; /* data offset */
1081 tx.len += 2;
1082 tx.buffer[tx.len++] = send->setup_count; /* setup count */
1083 tx.buffer[tx.len++] = 0; /* reserved3 */
1085 memcpy(&tx.buffer[tx.len], send->setup, send->setup_count*2); /* setup */
1086 tx.len += send->setup_count*2;
1088 /* add string here when implementing SMB_COM_TRANS */
1090 SMB_ADDWORD(&tx.buffer[param_ofs], tx.len);
1091 memcpy(&tx.buffer[tx.len], send->params, send->param_count); /* parameters */
1092 tx.len += send->param_count;
1093 if(tx.len%2)
1094 tx.len ++; /* pad2 */
1096 SMB_ADDWORD(&tx.buffer[data_ofs], tx.len);
1097 if(send->data_count && send->data)
1099 memcpy(&tx.buffer[tx.len], send->data, send->data_count); /* data */
1100 tx.len += send->data_count;
1103 recv->buf.buffer = NULL;
1104 recv->buf.len = 0;
1105 if(!NB_Transaction(fd, &tx, &recv->buf))
1106 goto done;
1108 if(!recv->buf.buffer)
1109 goto done;
1111 if(SMB_GetError(recv->buf.buffer))
1112 goto done;
1114 /* reuse these two offsets to check the received message */
1115 param_ofs = SMB_PARAM(recv->buf.buffer,4);
1116 data_ofs = SMB_PARAM(recv->buf.buffer,7);
1118 if( (recv->param_count + param_ofs) > recv->buf.len )
1119 goto done;
1121 if( (recv->data_count + data_ofs) > recv->buf.len )
1122 goto done;
1124 TRACE("Success\n");
1126 recv->setup = NULL;
1127 recv->setup_count = 0;
1129 recv->param_count = SMB_PARAM(recv->buf.buffer,0);
1130 recv->params = &recv->buf.buffer[param_ofs];
1132 recv->data_count = SMB_PARAM(recv->buf.buffer,6);
1133 recv->data = &recv->buf.buffer[data_ofs];
1136 TRACE("%d words\n",SMB_PARAM_COUNT(recv->buf.buffer));
1137 TRACE("total parameters = %d\n",SMB_PARAM(recv->buf.buffer,0));
1138 TRACE("total data = %d\n",SMB_PARAM(recv->buf.buffer,1));
1139 TRACE("parameters = %d\n",SMB_PARAM(recv->buf.buffer,3));
1140 TRACE("parameter offset = %d\n",SMB_PARAM(recv->buf.buffer,4));
1141 TRACE("param displace = %d\n",SMB_PARAM(recv->buf.buffer,5));
1143 TRACE("data count = %d\n",SMB_PARAM(recv->buf.buffer,6));
1144 TRACE("data offset = %d\n",SMB_PARAM(recv->buf.buffer,7));
1145 TRACE("data displace = %d\n",SMB_PARAM(recv->buf.buffer,8));
1148 ret = TRUE;
1150 done:
1151 if(tx.buffer)
1152 HeapFree(GetProcessHeap(),0,tx.buffer);
1154 return ret;
1157 static BOOL SMB_SetupFindFirst(struct SMB_Trans2Info *send, LPSTR filename)
1159 int search_attribs = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
1160 int search_count = 10;
1161 int flags = 0;
1162 int infolevel = 0x104; /* SMB_FILE_BOTH_DIRECTORY_INFO */
1163 int storagetype = 0;
1164 int len, buf_size;
1166 memset(send,0,sizeof send);
1168 send->setup_count = 1;
1169 send->setup = HeapAlloc(GetProcessHeap(),0,send->setup_count*2);
1170 if(!send->setup)
1171 return FALSE;
1173 buf_size = 0x10 + lstrlenA(filename);
1174 send->params = HeapAlloc(GetProcessHeap(),0,buf_size);
1175 if(!send->params)
1177 HeapFree(GetProcessHeap(),0,send->setup);
1178 return FALSE;
1181 SMB_ADDWORD(send->setup,TRANS2_FIND_FIRST2);
1183 len = 0;
1184 memset(send->params,0,buf_size);
1185 SMB_ADDWORD(&send->params[len],search_attribs); len += 2;
1186 SMB_ADDWORD(&send->params[len],search_count); len += 2;
1187 SMB_ADDWORD(&send->params[len],flags); len += 2;
1188 SMB_ADDWORD(&send->params[len],infolevel); len += 2;
1189 SMB_ADDDWORD(&send->params[len],storagetype); len += 4;
1191 strcpy(&send->params[len],filename);
1192 len += lstrlenA(filename)+1;
1194 send->param_count = len;
1195 send->data = NULL;
1196 send->data_count = 0;
1198 return TRUE;
1201 static SMB_DIR *SMB_Trans2FindFirst(int fd, USHORT tree_id,
1202 USHORT user_id, USHORT dialect, LPSTR filename )
1204 int num;
1205 BOOL ret;
1206 /* char *filename = "\\*"; */
1207 struct SMB_Trans2Info send, recv;
1208 SMB_DIR *smbdir = NULL;
1210 TRACE("patern = %s\n",filename);
1212 if(!SMB_SetupFindFirst(&send, filename))
1213 return FALSE;
1215 memset(&recv,0,sizeof recv);
1217 ret = SMB_Transaction2(fd, tree_id, user_id, &send, &recv);
1218 HeapFree(GetProcessHeap(),0,send.params);
1219 HeapFree(GetProcessHeap(),0,send.setup);
1221 if(!ret)
1222 goto done;
1224 if(recv.setup_count)
1225 goto done;
1227 if(recv.param_count != 10)
1228 goto done;
1230 num = SMB_GETWORD(&recv.params[2]);
1231 TRACE("Success, search id: %d\n",num);
1233 if(SMB_GETWORD(&recv.params[4]))
1234 FIXME("need to read more!\n");
1236 smbdir = HeapAlloc(GetProcessHeap(),0,sizeof(*smbdir));
1237 if(smbdir)
1239 int i, ofs=0;
1241 smbdir->current = 0;
1242 smbdir->num_entries = num;
1243 smbdir->entries = HeapAlloc(GetProcessHeap(), 0, sizeof(unsigned char*)*num);
1244 if(!smbdir->entries)
1245 goto done;
1246 smbdir->buffer = recv.buf.buffer; /* save to free later */
1248 for(i=0; i<num; i++)
1250 int size = SMB_GETDWORD(&recv.data[ofs]);
1252 smbdir->entries[i] = &recv.data[ofs];
1254 if(TRACE_ON(file))
1256 int j;
1257 for(j=0; j<size; j++)
1258 DPRINTF("%02x%c",recv.data[ofs+j],((j+1)%16)?' ':'\n');
1260 TRACE("file %d : %s\n", i, &recv.data[ofs+0x5e]);
1261 ofs += size;
1262 if(ofs>recv.data_count)
1263 goto done;
1266 ret = TRUE;
1269 done:
1270 if(!ret)
1272 if( recv.buf.buffer )
1273 HeapFree(GetProcessHeap(),0,recv.buf.buffer);
1274 if( smbdir )
1276 if( smbdir->entries )
1277 HeapFree(GetProcessHeap(),0,smbdir->entries);
1278 HeapFree(GetProcessHeap(),0,smbdir);
1280 smbdir = NULL;
1283 return smbdir;
1286 static int SMB_GetSocket(LPCSTR host)
1288 int fd=-1,r;
1289 struct sockaddr_in sin;
1290 struct hostent *he;
1292 TRACE("host %s\n",host);
1294 he = gethostbyname(host);
1295 if(he)
1297 memcpy(&sin.sin_addr,he->h_addr, sizeof (sin.sin_addr));
1298 goto connect;
1301 if(NB_Lookup(host,&sin))
1302 goto connect;
1304 /* FIXME: resolve by WINS too */
1306 ERR("couldn't resolve SMB host %s\n", host);
1308 return -1;
1310 connect:
1311 sin.sin_family = AF_INET;
1312 sin.sin_port = htons(139); /* netbios session */
1314 fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
1315 if(fd<0)
1316 return fd;
1319 unsigned char *x = (unsigned char *)&sin.sin_addr;
1320 TRACE("Connecting to %d.%d.%d.%d ...\n", x[0],x[1],x[2],x[3]);
1322 r = connect(fd, &sin, sizeof sin);
1324 if(!NB_SessionReq(fd, "*SMBSERVER", "WINE"))
1326 close(fd);
1327 return -1;
1330 return fd;
1333 static BOOL SMB_LoginAndConnect(int fd, LPCSTR host, LPCSTR share, USHORT *tree_id, USHORT *user_id, USHORT *dialect)
1335 LPSTR name=NULL;
1337 TRACE("host %s share %s\n",host,share);
1339 if(!SMB_NegotiateProtocol(fd, dialect))
1340 return FALSE;
1342 if(!SMB_SessionSetup(fd, user_id))
1343 return FALSE;
1345 name = HeapAlloc(GetProcessHeap(),0,strlen(host)+strlen(share)+5);
1346 if(!name)
1347 return FALSE;
1349 sprintf(name,"\\\\%s\\%s",host,share);
1350 if(!SMB_TreeConnect(fd,*user_id,name,tree_id))
1352 HeapFree(GetProcessHeap(),0,name);
1353 return FALSE;
1356 return TRUE;
1359 static HANDLE SMB_RegisterFile( int fd, USHORT tree_id, USHORT user_id, USHORT dialect, USHORT file_id)
1361 int r;
1362 HANDLE ret;
1364 wine_server_send_fd( fd );
1366 SERVER_START_REQ( create_smb )
1368 req->tree_id = tree_id;
1369 req->user_id = user_id;
1370 req->file_id = file_id;
1371 req->dialect = 0;
1372 req->fd = fd;
1373 SetLastError(0);
1374 r = wine_server_call_err( req );
1375 ret = reply->handle;
1377 SERVER_END_REQ;
1379 if(!r)
1380 TRACE("created wineserver smb object, handle = %04x\n",ret);
1381 else
1382 SetLastError( ERROR_PATH_NOT_FOUND );
1384 return ret;
1387 HANDLE WINAPI SMB_CreateFileA( LPCSTR uncname, DWORD access, DWORD sharing,
1388 LPSECURITY_ATTRIBUTES sa, DWORD creation,
1389 DWORD attributes, HANDLE template )
1391 int fd;
1392 USHORT tree_id=0, user_id=0, dialect=0, file_id=0;
1393 LPSTR name,host,share,file;
1394 HANDLE handle = INVALID_HANDLE_VALUE;
1396 name = HeapAlloc(GetProcessHeap(),0,lstrlenA(uncname));
1397 if(!name)
1398 return handle;
1400 lstrcpyA(name,uncname);
1402 if( !UNC_SplitName(name, &host, &share, &file) )
1404 HeapFree(GetProcessHeap(),0,name);
1405 return handle;
1408 TRACE("server is %s, share is %s, file is %s\n", host, share, file);
1410 fd = SMB_GetSocket(host);
1411 if(fd < 0)
1412 goto done;
1414 if(!SMB_LoginAndConnect(fd, host, share, &tree_id, &user_id, &dialect))
1415 goto done;
1417 #if 0
1418 if(!SMB_NtCreateOpen(fd, tree_id, user_id, dialect, file,
1419 access, sharing, sa, creation, attributes, template, &file_id ))
1421 close(fd);
1422 ERR("CreateOpen failed\n");
1423 goto done;
1425 #endif
1426 if(!SMB_Open(fd, tree_id, user_id, dialect, file,
1427 access, sharing, creation, attributes, &file_id ))
1429 close(fd);
1430 ERR("CreateOpen failed\n");
1431 goto done;
1434 handle = SMB_RegisterFile(fd, tree_id, user_id, dialect, file_id);
1435 if(!handle)
1437 ERR("register failed\n");
1438 close(fd);
1441 done:
1442 HeapFree(GetProcessHeap(),0,name);
1443 return handle;
1446 static BOOL SMB_GetSmbInfo(HANDLE hFile, USHORT *tree_id, USHORT *user_id, USHORT *dialect, USHORT *file_id, LPDWORD offset)
1448 int r;
1450 SERVER_START_REQ( get_smb_info )
1452 req->handle = hFile;
1453 req->flags = 0;
1454 SetLastError(0);
1455 r = wine_server_call_err( req );
1456 if(tree_id)
1457 *tree_id = reply->tree_id;
1458 if(user_id)
1459 *user_id = reply->user_id;
1460 if(file_id)
1461 *file_id = reply->file_id;
1462 if(dialect)
1463 *dialect = reply->dialect;
1464 if(offset)
1465 *offset = reply->offset;
1467 SERVER_END_REQ;
1469 return !r;
1472 static BOOL SMB_SetOffset(HANDLE hFile, DWORD offset)
1474 int r;
1476 TRACE("offset = %08lx\n",offset);
1478 SERVER_START_REQ( get_smb_info )
1480 req->handle = hFile;
1481 req->flags = SMBINFO_SET_OFFSET;
1482 req->offset = offset;
1483 SetLastError(0);
1484 r = wine_server_call_err( req );
1485 /* if(offset)
1486 *offset = reply->offset; */
1488 SERVER_END_REQ;
1490 return !r;
1493 BOOL WINAPI SMB_ReadFile(HANDLE hFile, LPVOID buffer, DWORD bytesToRead, LPDWORD bytesRead, LPOVERLAPPED lpOverlapped)
1495 int fd;
1496 DWORD total, count, offset;
1497 USHORT user_id, tree_id, dialect, file_id, read;
1498 BOOL r=TRUE;
1500 TRACE("%04x %p %ld %p\n", hFile, buffer, bytesToRead, bytesRead);
1502 if(!SMB_GetSmbInfo(hFile, &tree_id, &user_id, &dialect, &file_id, &offset))
1503 return FALSE;
1505 fd = FILE_GetUnixHandle(hFile, GENERIC_READ);
1506 if(fd<0)
1507 return FALSE;
1509 total = 0;
1510 while(1)
1512 count = bytesToRead - total;
1513 if(count>0x400)
1514 count = 0x400;
1515 if(count==0)
1516 break;
1517 read = 0;
1518 r = SMB_Read(fd, tree_id, user_id, dialect, file_id, offset, buffer, count, &read);
1519 if(!r)
1520 break;
1521 if(!read)
1522 break;
1523 total += read;
1524 buffer = (char*)buffer + read;
1525 offset += read;
1526 if(total>=bytesToRead)
1527 break;
1529 close(fd);
1531 if(bytesRead)
1532 *bytesRead = total;
1534 if(!SMB_SetOffset(hFile, offset))
1535 return FALSE;
1537 return r;
1540 SMB_DIR* WINAPI SMB_FindFirst(LPCSTR name)
1542 int fd = -1;
1543 LPSTR host,share,file;
1544 USHORT tree_id=0, user_id=0, dialect=0;
1545 SMB_DIR *ret = NULL;
1546 LPSTR filename;
1548 TRACE("Find %s\n",debugstr_a(name));
1550 filename = HeapAlloc(GetProcessHeap(),0,lstrlenA(name)+1);
1551 if(!filename)
1552 return ret;
1554 lstrcpyA(filename,name);
1556 if( !UNC_SplitName(filename, &host, &share, &file) )
1557 goto done;
1559 fd = SMB_GetSocket(host);
1560 if(fd < 0)
1561 goto done;
1563 if(!SMB_LoginAndConnect(fd, host, share, &tree_id, &user_id, &dialect))
1564 goto done;
1566 TRACE("server is %s, share is %s, file is %s\n", host, share, file);
1568 ret = SMB_Trans2FindFirst(fd, tree_id, user_id, dialect, file);
1570 done:
1571 /* disconnect */
1572 if(fd != -1)
1573 close(fd);
1575 if(filename)
1576 HeapFree(GetProcessHeap(),0,filename);
1578 return ret;
1582 BOOL WINAPI SMB_FindNext(SMB_DIR *dir, WIN32_FIND_DATAA *data )
1584 unsigned char *ent;
1585 int len, fnlen;
1587 TRACE("%d of %d\n",dir->current,dir->num_entries);
1589 if(dir->current >= dir->num_entries)
1590 return FALSE;
1592 memset(data, 0, sizeof *data);
1594 ent = dir->entries[dir->current];
1595 len = SMB_GETDWORD(&ent[0]);
1596 if(len<0x5e)
1597 return FALSE;
1599 memcpy(&data->ftCreationTime, &ent[8], 8);
1600 memcpy(&data->ftLastAccessTime, &ent[0x10], 8);
1601 memcpy(&data->ftLastWriteTime, &ent[0x18], 8);
1602 data->nFileSizeHigh = SMB_GETDWORD(&ent[0x30]);
1603 data->nFileSizeLow = SMB_GETDWORD(&ent[0x34]);
1604 data->dwFileAttributes = SMB_GETDWORD(&ent[0x38]);
1606 /* copy the long filename */
1607 fnlen = SMB_GETDWORD(&ent[0x3c]);
1608 if ( fnlen > (sizeof data->cFileName/sizeof(CHAR)) )
1609 return FALSE;
1610 memcpy(data->cFileName, &ent[0x5e], fnlen);
1612 /* copy the short filename */
1613 if ( ent[0x44] > (sizeof data->cAlternateFileName/sizeof(CHAR)) )
1614 return FALSE;
1615 memcpy(data->cAlternateFileName, &ent[0x5e + len], ent[0x44]);
1617 dir->current++;
1619 return TRUE;
1622 BOOL WINAPI SMB_CloseDir(SMB_DIR *dir)
1624 HeapFree(GetProcessHeap(),0,dir->buffer);
1625 HeapFree(GetProcessHeap(),0,dir->entries);
1626 memset(dir,0,sizeof *dir);
1627 HeapFree(GetProcessHeap(),0,dir);
1628 return TRUE;