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