Fix use of LOCALE_NOUSEROVERRIDE flag in GetNumberFormatA and
[wine.git] / files / smb.c
blob5e092e7953856c1c3a086cc522430a55968ccd61
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 #include "config.h"
32 #include "wine/port.h"
34 #include <assert.h>
35 #include <ctype.h>
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include <string.h>
41 #ifdef HAVE_SYS_ERRNO_H
42 #include <sys/errno.h>
43 #endif
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #ifdef HAVE_SYS_MMAN_H
47 #include <sys/mman.h>
48 #endif
49 #include <sys/time.h>
50 #include <sys/poll.h>
51 #include <time.h>
52 #include <unistd.h>
53 #include <utime.h>
54 #ifdef HAVE_SYS_SOCKET_H
55 # include <sys/socket.h>
56 #endif
57 #include <sys/types.h>
58 #ifdef HAVE_NETINET_IN_SYSTM_H
59 #include <netinet/in_systm.h>
60 #endif
61 #ifdef HAVE_NETINET_IN_H
62 #include <netinet/in.h>
63 #endif
64 #ifdef HAVE_NETINET_IP_H
65 #include <netinet/ip.h>
66 #endif
67 #ifdef HAVE_ARPA_INET_H
68 #include <arpa/inet.h>
69 #endif
70 #ifdef HAVE_NETDB_H
71 #include <netdb.h>
72 #endif
74 #include "winerror.h"
75 #include "windef.h"
76 #include "winbase.h"
77 #include "file.h"
78 #include "heap.h"
80 #include "smb.h"
82 #include "wine/server.h"
83 #include "wine/debug.h"
85 WINE_DEFAULT_DEBUG_CHANNEL(file);
87 #define MAX_HOST_NAME 15
88 #define NB_TIMEOUT 10000
90 USHORT SMB_MultiplexId = 0;
92 static int netbios_name(const char *p, unsigned char *buffer)
94 char ch;
95 int i,len=0;
97 buffer[len++]=' ';
98 for(i=0; i<=MAX_HOST_NAME; i++)
100 if(i<MAX_HOST_NAME)
102 if(*p)
103 ch = *p++&0xdf; /* add character from hostname */
104 else
105 ch = ' '; /* add padding */
107 else
108 ch = 0; /* add terminator */
109 buffer[len++] = ((ch&0xf0) >> 4) + 'A';
110 buffer[len++] = (ch&0x0f) + 'A';
112 buffer[len++] = 0; /* add second terminator */
113 return len;
116 static DWORD NB_NameReq(LPCSTR host, unsigned char *buffer, int len)
118 int trn = 1234,i=0;
120 NBR_ADDWORD(&buffer[i],trn); i+=2;
121 NBR_ADDWORD(&buffer[i],0x0110); i+=2;
122 NBR_ADDWORD(&buffer[i],0x0001); i+=2;
123 NBR_ADDWORD(&buffer[i],0x0000); i+=2;
124 NBR_ADDWORD(&buffer[i],0x0000); i+=2;
125 NBR_ADDWORD(&buffer[i],0x0000); i+=2;
127 i += netbios_name(host,&buffer[i]);
129 NBR_ADDWORD(&buffer[i],0x0020); i+=2;
130 NBR_ADDWORD(&buffer[i],0x0001); i+=2;
132 ERR("packet is %d bytes in length\n",i);
135 int j;
136 for(j=0; j<i; j++)
137 printf("%02x%c",buffer[j],(((j+1)%16)&&((j+1)!=j))?' ':'\n');
140 return i;
143 /* unc = \\hostname\share\file... */
144 static BOOL UNC_SplitName(LPSTR unc, LPSTR *hostname, LPSTR *share, LPSTR *file)
146 char *p;
148 ERR("%s\n",unc);
150 p = strchr(unc,'\\');
151 if(!p)
152 return FALSE;
153 p = strchr(p+1,'\\');
154 if(!p)
155 return FALSE;
156 *hostname=++p;
158 p = strchr(p,'\\');
159 if(!p)
160 return FALSE;
161 *p=0;
162 *share = ++p;
164 p = strchr(p,'\\');
165 if(!p)
166 return FALSE;
167 *p=0;
168 *file = ++p;
170 return TRUE;
173 static BOOL NB_Lookup(LPCSTR host, struct sockaddr_in *addr)
175 int fd,on=1,r,len,i,fromsize;
176 struct pollfd fds;
177 struct sockaddr_in sin,fromaddr;
178 unsigned char buffer[256];
180 fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
181 if(fd<0)
182 return FALSE;
184 r = setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &on, sizeof on);
185 if(r<0)
186 goto err;
188 if(0==inet_aton("255.255.255.255", (struct in_addr *)&sin.sin_addr.s_addr))
190 FIXME("Error getting bcast address\n");
191 goto err;
193 sin.sin_family = AF_INET;
194 sin.sin_port = htons(137);
196 len = NB_NameReq(host,buffer,sizeof buffer);
197 if(len<=0)
198 goto err;
200 r = sendto(fd, buffer, len, 0, &sin, sizeof sin);
201 if(r<0)
203 FIXME("Error sending packet\n");
204 goto err;
207 fds.fd = fd;
208 fds.events = POLLIN;
209 fds.revents = 0;
211 /* FIXME: this is simple and easily fooled logic
212 * we should loop until we receive the correct packet or timeout
214 r = poll(&fds,1,NB_TIMEOUT);
215 if(r!=1)
216 goto err;
218 TRACE("Got response!\n");
220 fromsize = sizeof (fromaddr);
221 r = recvfrom(fd, buffer, sizeof buffer, 0, &fromaddr, &fromsize);
222 if(r<0)
223 goto err;
225 ERR("%d bytes received\n",r);
227 if(r!=62)
228 goto err;
230 for(i=0; i<r; i++)
231 DPRINTF("%02X%c",buffer[i],(((i+1)!=r)&&((i+1)%16))?' ':'\n');
232 DPRINTF("\n");
234 if(0x0f & buffer[3])
235 goto err;
237 ERR("packet is OK\n");
239 memcpy(&addr->sin_addr, &buffer[58], sizeof addr->sin_addr);
241 close(fd);
242 return TRUE;
244 err:
245 close(fd);
246 return FALSE;
249 #define NB_FIRST 0x40
251 #define NB_HDRSIZE 4
253 #define NB_SESSION_MSG 0x00
254 #define NB_SESSION_REQ 0x81
256 /* RFC 1002, section 4.3.2 */
257 static BOOL NB_SessionReq(int fd, char *called, char *calling)
259 unsigned char buffer[0x100];
260 int len = 0,r;
261 struct pollfd fds;
263 ERR("called %s, calling %s\n",called,calling);
265 buffer[0] = NB_SESSION_REQ;
266 buffer[1] = NB_FIRST;
268 netbios_name(called, &buffer[NB_HDRSIZE]);
269 len += 34;
270 netbios_name(calling, &buffer[NB_HDRSIZE+len]);
271 len += 34;
273 NBR_ADDWORD(&buffer[2],len);
275 /* for(i=0; i<(len+NB_HDRSIZE); i++)
276 DPRINTF("%02X%c",buffer[i],(((i+1)!=(len+4))&&((i+1)%16))?' ':'\n'); */
278 r = write(fd,buffer,len+4);
279 if(r<0)
281 ERR("Write failed\n");
282 return FALSE;
285 fds.fd = fd;
286 fds.events = POLLIN;
287 fds.revents = 0;
289 r = poll(&fds,1,NB_TIMEOUT);
290 if(r!=1)
292 ERR("Poll failed\n");
293 return FALSE;
296 r = read(fd, buffer, NB_HDRSIZE);
297 if((r!=NB_HDRSIZE) || (buffer[0]!=0x82))
299 ERR("Received %d bytes\n",r);
300 ERR("%02x %02x %02x %02x\n", buffer[0],buffer[1],buffer[2],buffer[3]);
301 return FALSE;
304 return TRUE;
307 static BOOL NB_SendData(int fd, unsigned char *data, int size)
309 unsigned char buffer[NB_HDRSIZE];
310 int r;
312 /* CHECK: is it always OK to do this in two writes? */
313 /* perhaps use scatter gather sendmsg instead? */
315 buffer[0] = NB_SESSION_MSG;
316 buffer[1] = NB_FIRST;
317 NBR_ADDWORD(&buffer[2],size);
319 r = write(fd, buffer, NB_HDRSIZE);
320 if(r!=NB_HDRSIZE)
321 return FALSE;
323 r = write(fd, data, size);
324 if(r!=size)
326 ERR("write failed\n");
327 return FALSE;
330 return TRUE;
333 static BOOL NB_RecvData(int fd, unsigned char *data, int *outlen)
335 int r,len;
336 unsigned char buffer[NB_HDRSIZE];
338 r = read(fd, buffer, NB_HDRSIZE);
339 if((r!=NB_HDRSIZE) || (buffer[0]!=NB_SESSION_MSG))
341 ERR("Received %d bytes\n",r);
342 return FALSE;
345 len = NBR_GETWORD(&buffer[2]);
346 r = read(fd, data, len);
347 if(len!=r)
349 ERR("Received %d bytes\n",r);
350 return FALSE;
352 *outlen = len;
354 return TRUE;
357 static BOOL NB_Transaction(int fd, unsigned char *buffer, int len, int *outlen)
359 int r,i;
360 struct pollfd fds;
362 DPRINTF("Sending request:\n");
363 for(i=0; i<len; i++)
364 DPRINTF("%02X%c",buffer[i],(((i+1)!=len)&&((i+1)%16))?' ':'\n');
366 if(!NB_SendData(fd,buffer,len))
367 return FALSE;
369 fds.fd = fd;
370 fds.events = POLLIN;
371 fds.revents = 0;
373 r = poll(&fds,1,NB_TIMEOUT);
374 if(r!=1)
376 ERR("Poll failed\n");
377 return FALSE;
380 if(!NB_RecvData(fd, buffer, outlen))
381 return FALSE;
383 len = *outlen;
384 DPRINTF("Got response:\n");
385 for(i=0; i<len; i++)
386 DPRINTF("%02X%c",buffer[i],(((i+1)!=len)&&((i+1)%16))?' ':'\n');
388 return TRUE;
391 #define SMB_ADDHEADER(b,l) { b[(l)++]=0xff; b[(l)++]='S'; b[(l)++]='M'; b[(l)++]='B'; }
392 #define SMB_ADDERRINFO(b,l) { b[(l)++]=0; b[(l)++]=0; b[(l)++]=0; b[(l)++]=0; }
393 #define SMB_ADDPADSIG(b,l) { memset(&b[l],0,12); l+=12; }
395 #define SMB_ERRCLASS 5
396 #define SMB_ERRCODE 7
397 #define SMB_TREEID 24
398 #define SMB_PROCID 26
399 #define SMB_USERID 28
400 #define SMB_PLEXID 30
401 #define SMB_PCOUNT 32
402 #define SMB_HDRSIZE 33
404 static DWORD SMB_GetError(unsigned char *buffer)
406 if(buffer[SMB_ERRCLASS]==0)
407 return STATUS_SUCCESS;
408 /* FIXME: return propper error codes */
409 return STATUS_INVALID_PARAMETER;
412 static int SMB_Header(unsigned char *buffer, unsigned char command, USHORT tree_id, USHORT user_id)
414 int len = 0;
415 DWORD id;
417 /* 0 */
418 SMB_ADDHEADER(buffer,len);
420 /* 4 */
421 buffer[len++] = command;
423 /* 5 */
424 SMB_ADDERRINFO(buffer,len)
426 /* 9 */
427 buffer[len++] = 0x00; /* flags */
428 SMB_ADDWORD(&buffer[len],1); len += 2; /* flags2 */
430 /* 12 */
431 SMB_ADDPADSIG(buffer,len)
433 /* 24 */
434 SMB_ADDWORD(&buffer[len],tree_id); len += 2; /* treeid */
435 id = GetCurrentThreadId();
436 SMB_ADDWORD(&buffer[len],id); len += 2; /* process id */
437 SMB_ADDWORD(&buffer[len],user_id); len += 2; /* user id */
438 SMB_ADDWORD(&buffer[len],SMB_MultiplexId); len += 2; /* multiplex id */
439 SMB_MultiplexId++;
441 return len;
444 static const char *SMB_ProtocolDialect = "NT LM 0.12";
445 /* = "Windows for Workgroups 3.1a"; */
447 /* FIXME: support multiple SMB dialects */
448 static BOOL SMB_NegotiateProtocol(int fd, USHORT *dialect)
450 unsigned char buffer[0x100];
451 int buflen,len = 0;
453 ERR("\n");
455 memset(buffer,0,sizeof buffer);
457 len = SMB_Header(buffer, SMB_COM_NEGOTIATE, 0, 0);
459 /* parameters */
460 buffer[len++] = 0; /* no parameters */
462 /* command buffer */
463 buflen = strlen(SMB_ProtocolDialect)+2; /* include type and nul byte */
464 SMB_ADDWORD(&buffer[len],buflen); len += 2;
466 buffer[len] = 0x02;
467 strcpy(&buffer[len+1],SMB_ProtocolDialect);
468 len += buflen;
470 if(!NB_Transaction(fd, buffer, len, &len))
472 ERR("Failed\n");
473 return FALSE;
476 /* FIXME: check response */
477 if(SMB_GetError(buffer))
479 ERR("returned error\n");
480 return FALSE;
483 *dialect = 0;
485 return TRUE;
488 #define SMB_PARAM_COUNT(buffer) ((buffer)[SMB_PCOUNT])
489 #define SMB_PARAM(buffer,n) SMB_GETWORD(&(buffer)[SMB_HDRSIZE+2*(n)])
490 #define SMB_BUFFER_COUNT(buffer) SMB_GETWORD(buffer+SMB_HDRSIZE+2*SMB_PARAM_COUNT(buffer))
491 #define SMB_BUFFER(buffer,n) ((buffer)[SMB_HDRSIZE + 2*SMB_PARAM_COUNT(buffer) + 2 + (n) ])
493 static BOOL SMB_SessionSetup(int fd, USHORT *userid)
495 unsigned char buffer[0x100];
496 int len = 0;
497 int i,pcount,bcount;
499 memset(buffer,0,sizeof buffer);
501 len = SMB_Header(buffer, SMB_COM_SESSION_SETUP_ANDX, 0, 0);
503 buffer[len++] = 0; /* no parameters? */
505 buffer[len++] = 0xff; /* AndXCommand: secondary request */
506 buffer[len++] = 0x00; /* AndXReserved */
507 SMB_ADDWORD(&buffer[len],0); len += 2; /* AndXOffset */
508 SMB_ADDWORD(&buffer[len],0x400); len += 2; /* MaxBufferSize */
509 SMB_ADDWORD(&buffer[len],1); len += 2; /* MaxMpxCount */
510 SMB_ADDWORD(&buffer[len],0); len += 2; /* VcNumber */
511 SMB_ADDWORD(&buffer[len],0); len += 2; /* SessionKey */
512 SMB_ADDWORD(&buffer[len],0); len += 2; /* SessionKey */
513 SMB_ADDWORD(&buffer[len],0); len += 2; /* Password length */
514 SMB_ADDWORD(&buffer[len],0); len += 2; /* Reserved */
515 SMB_ADDWORD(&buffer[len],0); len += 2; /* Reserved */
517 /* FIXME: add name and password here */
518 buffer[len++] = 0; /* number of bytes in password */
520 if(!NB_Transaction(fd, buffer, len, &len))
521 return FALSE;
523 if(SMB_GetError(buffer))
524 return FALSE;
526 pcount = SMB_PARAM_COUNT(buffer);
528 if( (SMB_HDRSIZE+pcount*2) > len )
530 ERR("Bad parameter count %d\n",pcount);
531 return FALSE;
534 DPRINTF("SMB_COM_SESSION_SETUP response, %d args: ",pcount);
535 for(i=0; i<pcount; i++)
536 DPRINTF("%04x ",SMB_PARAM(buffer,i));
537 DPRINTF("\n");
539 bcount = SMB_BUFFER_COUNT(buffer);
540 if( (SMB_HDRSIZE+pcount*2+2+bcount) > len )
542 ERR("parameter count %x, buffer count %x, len %x\n",pcount,bcount,len);
543 return FALSE;
546 DPRINTF("response buffer %d bytes: ",bcount);
547 for(i=0; i<bcount; i++)
549 unsigned char ch = SMB_BUFFER(buffer,i);
550 DPRINTF("%c", isprint(ch)?ch:' ');
552 DPRINTF("\n");
554 *userid = SMB_GETWORD(&buffer[SMB_USERID]);
556 return TRUE;
559 static BOOL SMB_TreeConnect(int fd, USHORT user_id, LPCSTR share_name, USHORT *treeid)
561 unsigned char buffer[0x100];
562 int len = 0,slen;
564 ERR("%s\n",share_name);
566 memset(buffer,0,sizeof buffer);
568 len = SMB_Header(buffer, SMB_COM_TREE_CONNECT, 0, user_id);
570 buffer[len++] = 4; /* parameters */
572 buffer[len++] = 0xff; /* AndXCommand: secondary request */
573 buffer[len++] = 0x00; /* AndXReserved */
574 SMB_ADDWORD(&buffer[len],0); len += 2; /* AndXOffset */
575 SMB_ADDWORD(&buffer[len],0); len += 2; /* Flags */
576 SMB_ADDWORD(&buffer[len],1); len += 2; /* Password length */
578 /* SMB command buffer */
579 SMB_ADDWORD(&buffer[len],3); len += 2; /* command buffer len */
580 buffer[len++] = 0; /* null terminated password */
582 slen = strlen(share_name);
583 if(slen<(sizeof buffer-len))
584 strcpy(&buffer[len], share_name);
585 else
586 return FALSE;
587 len += slen+1;
589 /* name of the service */
590 buffer[len++] = 0;
592 if(!NB_Transaction(fd, buffer, len, &len))
593 return FALSE;
595 if(SMB_GetError(buffer))
596 return FALSE;
598 *treeid = SMB_GETWORD(&buffer[SMB_TREEID]);
600 ERR("OK, treeid = %04x\n", *treeid);
602 return TRUE;
605 static BOOL SMB_NtCreateOpen(int fd, USHORT tree_id, USHORT user_id, USHORT dialect,
606 LPCSTR filename, DWORD access, DWORD sharing,
607 LPSECURITY_ATTRIBUTES sa, DWORD creation,
608 DWORD attributes, HANDLE template, USHORT *file_id )
610 unsigned char buffer[0x100];
611 int len = 0,slen;
613 ERR("%s\n",filename);
615 memset(buffer,0,sizeof buffer);
617 len = SMB_Header(buffer, SMB_COM_NT_CREATE_ANDX, tree_id, user_id);
619 /* 0 */
620 buffer[len++] = 24; /* parameters */
622 buffer[len++] = 0xff; /* AndXCommand: secondary request */
623 buffer[len++] = 0x00; /* AndXReserved */
624 SMB_ADDWORD(&buffer[len],0); len += 2; /* AndXOffset */
626 buffer[len++] = 0; /* reserved */
627 slen = strlen(filename);
628 SMB_ADDWORD(&buffer[len],slen); len += 2; /* name length */
630 /* 0x08 */
631 SMB_ADDDWORD(&buffer[len],0); len += 4; /* flags */
632 SMB_ADDDWORD(&buffer[len],0); len += 4; /* root directory fid */
633 /* 0x10 */
634 SMB_ADDDWORD(&buffer[len],access); len += 4; /* access */
635 SMB_ADDDWORD(&buffer[len],0); len += 4; /* allocation size */
636 /* 0x18 */
637 SMB_ADDDWORD(&buffer[len],0); len += 4; /* root directory fid */
639 /* 0x1c */
640 SMB_ADDDWORD(&buffer[len],0); len += 4; /* initial allocation */
641 SMB_ADDDWORD(&buffer[len],0); len += 4;
643 /* 0x24 */
644 SMB_ADDDWORD(&buffer[len],attributes); len += 4; /* ExtFileAttributes*/
646 /* 0x28 */
647 SMB_ADDDWORD(&buffer[len],sharing); len += 4; /* ShareAccess */
649 /* 0x2c */
650 ERR("creation = %08lx\n",creation);
651 SMB_ADDDWORD(&buffer[len],creation); len += 4; /* CreateDisposition */
653 /* 0x30 */
654 SMB_ADDDWORD(&buffer[len],creation); len += 4; /* CreateOptions */
656 /* 0x34 */
657 SMB_ADDDWORD(&buffer[len],0); len += 4; /* Impersonation */
659 /* 0x38 */
660 buffer[len++] = 0; /* security flags */
662 /* 0x39 */
663 SMB_ADDWORD(&buffer[len],slen); len += 2; /* size of buffer */
665 if(slen<(sizeof buffer-len))
666 strcpy(&buffer[len], filename);
667 else
668 return FALSE;
669 len += slen+1;
671 /* name of the file */
672 buffer[len++] = 0;
674 if(!NB_Transaction(fd, buffer, len, &len))
675 return FALSE;
677 if(SMB_GetError(buffer))
678 return FALSE;
680 ERR("OK\n");
682 /* FIXME */
683 /* *file_id = SMB_GETWORD(&buffer[xxx]); */
684 *file_id = 0;
685 return FALSE;
687 return TRUE;
690 static USHORT SMB_GetMode(DWORD access, DWORD sharing)
692 USHORT mode=0;
694 switch(access&(GENERIC_READ|GENERIC_WRITE))
696 case GENERIC_READ:
697 mode |= OF_READ;
698 break;
699 case GENERIC_WRITE:
700 mode |= OF_WRITE;
701 break;
702 case (GENERIC_READ|GENERIC_WRITE):
703 mode |= OF_READWRITE;
704 break;
707 switch(sharing&(FILE_SHARE_READ|FILE_SHARE_WRITE))
709 case (FILE_SHARE_READ|FILE_SHARE_WRITE):
710 mode |= OF_SHARE_DENY_NONE;
711 break;
712 case FILE_SHARE_READ:
713 mode |= OF_SHARE_DENY_WRITE;
714 break;
715 case FILE_SHARE_WRITE:
716 mode |= OF_SHARE_DENY_READ;
717 break;
718 default:
719 mode |= OF_SHARE_EXCLUSIVE;
720 break;
723 return mode;
726 /* inverse of FILE_ConvertOFMode */
727 static BOOL SMB_OpenAndX(int fd, USHORT tree_id, USHORT user_id, USHORT dialect,
728 LPCSTR filename, DWORD access, DWORD sharing,
729 DWORD creation, DWORD attributes, USHORT *file_id )
731 unsigned char buffer[0x100];
732 int len = 0;
733 USHORT mode;
735 ERR("%s\n",filename);
737 mode = SMB_GetMode(access,sharing);
739 memset(buffer,0,sizeof buffer);
741 len = SMB_Header(buffer, SMB_COM_OPEN_ANDX, tree_id, user_id);
743 /* 0 */
744 buffer[len++] = 15; /* parameters */
745 buffer[len++] = 0xff; /* AndXCommand: secondary request */
746 buffer[len++] = 0x00; /* AndXReserved */
747 SMB_ADDWORD(buffer+len,0); len+=2; /* AndXOffset */
748 SMB_ADDWORD(buffer+len,0); len+=2; /* Flags */
749 SMB_ADDWORD(buffer+len,mode); len+=2; /* desired access */
750 SMB_ADDWORD(buffer+len,0); len+=2; /* search attributes */
751 SMB_ADDWORD(buffer+len,0); len+=2;
753 /*FIXME: complete */
754 return FALSE;
757 static BOOL SMB_Open(int fd, USHORT tree_id, USHORT user_id, USHORT dialect,
758 LPCSTR filename, DWORD access, DWORD sharing,
759 DWORD creation, DWORD attributes, USHORT *file_id )
761 unsigned char buffer[0x100];
762 int len = 0,slen,pcount,i;
763 USHORT mode = SMB_GetMode(access,sharing);
765 ERR("%s\n",filename);
767 memset(buffer,0,sizeof buffer);
769 len = SMB_Header(buffer, SMB_COM_OPEN, tree_id, user_id);
771 /* 0 */
772 buffer[len++] = 2; /* parameters */
773 SMB_ADDWORD(buffer+len,mode); len+=2;
774 SMB_ADDWORD(buffer+len,0); len+=2; /* search attributes */
776 slen = strlen(filename)+2; /* inc. nul and BufferFormat */
777 SMB_ADDWORD(buffer+len,slen); len+=2;
779 buffer[len] = 0x04; /* BufferFormat */
780 strcpy(&buffer[len+1],filename);
781 len += slen;
783 if(!NB_Transaction(fd, buffer, len, &len))
784 return FALSE;
786 if(SMB_GetError(buffer))
787 return FALSE;
789 pcount = SMB_PARAM_COUNT(buffer);
791 if( (SMB_HDRSIZE+pcount*2) > len )
793 ERR("Bad parameter count %d\n",pcount);
794 return FALSE;
797 ERR("response, %d args: ",pcount);
798 for(i=0; i<pcount; i++)
799 DPRINTF("%04x ",SMB_PARAM(buffer,i));
800 DPRINTF("\n");
802 *file_id = SMB_PARAM(buffer,0);
804 ERR("file_id = %04x\n",*file_id);
806 return TRUE;
809 static BOOL SMB_Read(int fd, USHORT tree_id, USHORT user_id, USHORT dialect, USHORT file_id, DWORD offset, LPVOID out, USHORT count, LPUSHORT read)
811 unsigned char *buffer;
812 int len,buf_size,n,i;
814 ERR("user %04x tree %04x file %04x count %04x offset %08lx\n",
815 user_id, tree_id, file_id, count, offset);
817 buf_size = count+0x100;
818 buffer = (unsigned char *) HeapAlloc(GetProcessHeap(),0,buf_size);
820 memset(buffer,0,buf_size);
822 len = SMB_Header(buffer, SMB_COM_READ, tree_id, user_id);
824 buffer[len++] = 5;
825 SMB_ADDWORD(&buffer[len],file_id); len += 2;
826 SMB_ADDWORD(&buffer[len],count); len += 2;
827 SMB_ADDDWORD(&buffer[len],offset); len += 4;
828 SMB_ADDWORD(&buffer[len],0); len += 2; /* how many more bytes will be read */
830 buffer[len++] = 0;
832 if(!NB_Transaction(fd, buffer, len, &len))
834 HeapFree(GetProcessHeap(),0,buffer);
835 return FALSE;
838 if(SMB_GetError(buffer))
840 HeapFree(GetProcessHeap(),0,buffer);
841 return FALSE;
844 n = SMB_PARAM_COUNT(buffer);
846 if( (SMB_HDRSIZE+n*2) > len )
848 HeapFree(GetProcessHeap(),0,buffer);
849 ERR("Bad parameter count %d\n",n);
850 return FALSE;
853 ERR("response, %d args: ",n);
854 for(i=0; i<n; i++)
855 DPRINTF("%04x ",SMB_PARAM(buffer,i));
856 DPRINTF("\n");
858 n = SMB_PARAM(buffer,5) - 3;
859 if(n>count)
860 n=count;
862 memcpy( out, &SMB_BUFFER(buffer,3), n);
864 ERR("Read %d bytes\n",n);
865 *read = n;
867 HeapFree(GetProcessHeap(),0,buffer);
869 return TRUE;
872 static int SMB_GetSocket(LPCSTR host)
874 int fd=-1,r;
875 struct sockaddr_in sin;
876 struct hostent *he;
878 ERR("host %s\n",host);
880 if(NB_Lookup(host,&sin))
881 goto connect;
883 he = gethostbyname(host);
884 if(he)
886 memcpy(&sin.sin_addr,he->h_addr, sizeof (sin.sin_addr));
887 goto connect;
890 /* FIXME: resolve by WINS too */
892 ERR("couldn't resolve SMB host %s\n", host);
894 return -1;
896 connect:
897 sin.sin_family = AF_INET;
898 sin.sin_port = htons(139); /* netbios session */
900 fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
901 if(fd<0)
902 return fd;
905 unsigned char *x = (unsigned char *)&sin.sin_addr;
906 ERR("Connecting to %d.%d.%d.%d ...\n", x[0],x[1],x[2],x[3]);
908 r = connect(fd, &sin, sizeof sin);
910 if(!NB_SessionReq(fd, "*SMBSERVER", "WINE"))
912 close(fd);
913 return -1;
916 return fd;
919 static BOOL SMB_LoginAndConnect(int fd, LPCSTR host, LPCSTR share, USHORT *tree_id, USHORT *user_id, USHORT *dialect)
921 LPSTR name=NULL;
923 ERR("host %s share %s\n",host,share);
925 if(!SMB_NegotiateProtocol(fd, dialect))
926 return FALSE;
928 if(!SMB_SessionSetup(fd, user_id))
929 return FALSE;
931 name = HeapAlloc(GetProcessHeap(),0,strlen(host)+strlen(share)+5);
932 if(!name)
933 return FALSE;
935 sprintf(name,"\\\\%s\\%s",host,share);
936 if(!SMB_TreeConnect(fd,*user_id,name,tree_id))
938 HeapFree(GetProcessHeap(),0,name);
939 return FALSE;
942 return TRUE;
945 static HANDLE SMB_RegisterFile( int fd, USHORT tree_id, USHORT user_id, USHORT dialect, USHORT file_id)
947 int r;
948 HANDLE ret;
950 wine_server_send_fd( fd );
952 SERVER_START_REQ( create_smb )
954 req->tree_id = tree_id;
955 req->user_id = user_id;
956 req->file_id = file_id;
957 req->dialect = 0;
958 req->fd = fd;
959 SetLastError(0);
960 r = wine_server_call_err( req );
961 ret = reply->handle;
963 SERVER_END_REQ;
965 if(!r)
966 ERR("created wineserver smb object, handle = %04x\n",ret);
967 else
968 SetLastError( ERROR_PATH_NOT_FOUND );
970 return ret;
973 HANDLE WINAPI SMB_CreateFileA( LPCSTR uncname, DWORD access, DWORD sharing,
974 LPSECURITY_ATTRIBUTES sa, DWORD creation,
975 DWORD attributes, HANDLE template )
977 int fd;
978 USHORT tree_id=0, user_id=0, dialect=0, file_id=0;
979 LPSTR name,host,share,file;
980 HANDLE handle = INVALID_HANDLE_VALUE;
982 name = HeapAlloc(GetProcessHeap(),0,lstrlenA(uncname));
983 if(!name)
984 return handle;
986 lstrcpyA(name,uncname);
988 if( !UNC_SplitName(name, &host, &share, &file) )
990 HeapFree(GetProcessHeap(),0,name);
991 return handle;
994 ERR("server is %s, share is %s, file is %s\n", host, share, file);
996 fd = SMB_GetSocket(host);
997 if(fd < 0)
998 goto done;
1000 if(!SMB_LoginAndConnect(fd, host, share, &tree_id, &user_id, &dialect))
1001 goto done;
1003 #if 0
1004 if(!SMB_NtCreateOpen(fd, tree_id, user_id, dialect, file,
1005 access, sharing, sa, creation, attributes, template, &file_id ))
1007 close(fd);
1008 ERR("CreateOpen failed\n");
1009 goto done;
1011 #endif
1012 if(!SMB_Open(fd, tree_id, user_id, dialect, file,
1013 access, sharing, creation, attributes, &file_id ))
1015 close(fd);
1016 ERR("CreateOpen failed\n");
1017 goto done;
1020 handle = SMB_RegisterFile(fd, tree_id, user_id, dialect, file_id);
1021 if(!handle)
1023 ERR("register failed\n");
1024 close(fd);
1027 done:
1028 HeapFree(GetProcessHeap(),0,name);
1029 return handle;
1032 static BOOL SMB_GetSmbInfo(HANDLE hFile, USHORT *tree_id, USHORT *user_id, USHORT *dialect, USHORT *file_id, LPDWORD offset)
1034 int r;
1036 SERVER_START_REQ( get_smb_info )
1038 req->handle = hFile;
1039 req->flags = 0;
1040 SetLastError(0);
1041 r = wine_server_call_err( req );
1042 if(tree_id)
1043 *tree_id = reply->tree_id;
1044 if(user_id)
1045 *user_id = reply->user_id;
1046 if(file_id)
1047 *file_id = reply->file_id;
1048 if(dialect)
1049 *dialect = reply->dialect;
1050 if(offset)
1051 *offset = reply->offset;
1053 SERVER_END_REQ;
1055 return !r;
1058 static BOOL SMB_SetOffset(HANDLE hFile, DWORD offset)
1060 int r;
1062 ERR("offset = %08lx\n",offset);
1064 SERVER_START_REQ( get_smb_info )
1066 req->handle = hFile;
1067 req->flags = SMBINFO_SET_OFFSET;
1068 req->offset = offset;
1069 SetLastError(0);
1070 r = wine_server_call_err( req );
1071 /* if(offset)
1072 *offset = reply->offset; */
1074 SERVER_END_REQ;
1076 return !r;
1079 BOOL WINAPI SMB_ReadFile(HANDLE hFile, LPVOID buffer, DWORD bytesToRead, LPDWORD bytesRead, LPOVERLAPPED lpOverlapped)
1081 int fd;
1082 DWORD total, count, offset;
1083 USHORT user_id, tree_id, dialect, file_id, read;
1084 BOOL r=TRUE;
1086 ERR("%04x %p %ld %p\n", hFile, buffer, bytesToRead, bytesRead);
1088 if(!SMB_GetSmbInfo(hFile, &tree_id, &user_id, &dialect, &file_id, &offset))
1089 return FALSE;
1091 fd = FILE_GetUnixHandle(hFile, GENERIC_READ);
1092 if(fd<0)
1093 return FALSE;
1095 total = 0;
1096 while(1)
1098 count = bytesToRead - total;
1099 if(count>0x400)
1100 count = 0x400;
1101 if(count==0)
1102 break;
1103 read = 0;
1104 r = SMB_Read(fd, tree_id, user_id, dialect, file_id, offset, buffer, count, &read);
1105 if(!r)
1106 break;
1107 if(!read)
1108 break;
1109 total += read;
1110 buffer += read;
1111 offset += read;
1112 if(total>=bytesToRead)
1113 break;
1115 close(fd);
1117 if(bytesRead)
1118 *bytesRead = total;
1120 if(!SMB_SetOffset(hFile, offset))
1121 return FALSE;
1123 return r;