Update output for recent build changes.
[wine/hacks.git] / files / smb.c
blob3fdb7d9e9eb88ddd5170527863e95d5d3bf880c5
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 #if 0 /* not yet */
606 static BOOL SMB_NtCreateOpen(int fd, USHORT tree_id, USHORT user_id, USHORT dialect,
607 LPCSTR filename, DWORD access, DWORD sharing,
608 LPSECURITY_ATTRIBUTES sa, DWORD creation,
609 DWORD attributes, HANDLE template, USHORT *file_id )
611 unsigned char buffer[0x100];
612 int len = 0,slen;
614 ERR("%s\n",filename);
616 memset(buffer,0,sizeof buffer);
618 len = SMB_Header(buffer, SMB_COM_NT_CREATE_ANDX, tree_id, user_id);
620 /* 0 */
621 buffer[len++] = 24; /* parameters */
623 buffer[len++] = 0xff; /* AndXCommand: secondary request */
624 buffer[len++] = 0x00; /* AndXReserved */
625 SMB_ADDWORD(&buffer[len],0); len += 2; /* AndXOffset */
627 buffer[len++] = 0; /* reserved */
628 slen = strlen(filename);
629 SMB_ADDWORD(&buffer[len],slen); len += 2; /* name length */
631 /* 0x08 */
632 SMB_ADDDWORD(&buffer[len],0); len += 4; /* flags */
633 SMB_ADDDWORD(&buffer[len],0); len += 4; /* root directory fid */
634 /* 0x10 */
635 SMB_ADDDWORD(&buffer[len],access); len += 4; /* access */
636 SMB_ADDDWORD(&buffer[len],0); len += 4; /* allocation size */
637 /* 0x18 */
638 SMB_ADDDWORD(&buffer[len],0); len += 4; /* root directory fid */
640 /* 0x1c */
641 SMB_ADDDWORD(&buffer[len],0); len += 4; /* initial allocation */
642 SMB_ADDDWORD(&buffer[len],0); len += 4;
644 /* 0x24 */
645 SMB_ADDDWORD(&buffer[len],attributes); len += 4; /* ExtFileAttributes*/
647 /* 0x28 */
648 SMB_ADDDWORD(&buffer[len],sharing); len += 4; /* ShareAccess */
650 /* 0x2c */
651 ERR("creation = %08lx\n",creation);
652 SMB_ADDDWORD(&buffer[len],creation); len += 4; /* CreateDisposition */
654 /* 0x30 */
655 SMB_ADDDWORD(&buffer[len],creation); len += 4; /* CreateOptions */
657 /* 0x34 */
658 SMB_ADDDWORD(&buffer[len],0); len += 4; /* Impersonation */
660 /* 0x38 */
661 buffer[len++] = 0; /* security flags */
663 /* 0x39 */
664 SMB_ADDWORD(&buffer[len],slen); len += 2; /* size of buffer */
666 if(slen<(sizeof buffer-len))
667 strcpy(&buffer[len], filename);
668 else
669 return FALSE;
670 len += slen+1;
672 /* name of the file */
673 buffer[len++] = 0;
675 if(!NB_Transaction(fd, buffer, len, &len))
676 return FALSE;
678 if(SMB_GetError(buffer))
679 return FALSE;
681 ERR("OK\n");
683 /* FIXME */
684 /* *file_id = SMB_GETWORD(&buffer[xxx]); */
685 *file_id = 0;
686 return FALSE;
688 return TRUE;
690 #endif
692 static USHORT SMB_GetMode(DWORD access, DWORD sharing)
694 USHORT mode=0;
696 switch(access&(GENERIC_READ|GENERIC_WRITE))
698 case GENERIC_READ:
699 mode |= OF_READ;
700 break;
701 case GENERIC_WRITE:
702 mode |= OF_WRITE;
703 break;
704 case (GENERIC_READ|GENERIC_WRITE):
705 mode |= OF_READWRITE;
706 break;
709 switch(sharing&(FILE_SHARE_READ|FILE_SHARE_WRITE))
711 case (FILE_SHARE_READ|FILE_SHARE_WRITE):
712 mode |= OF_SHARE_DENY_NONE;
713 break;
714 case FILE_SHARE_READ:
715 mode |= OF_SHARE_DENY_WRITE;
716 break;
717 case FILE_SHARE_WRITE:
718 mode |= OF_SHARE_DENY_READ;
719 break;
720 default:
721 mode |= OF_SHARE_EXCLUSIVE;
722 break;
725 return mode;
728 #if 0 /* not yet */
729 /* inverse of FILE_ConvertOFMode */
730 static BOOL SMB_OpenAndX(int fd, USHORT tree_id, USHORT user_id, USHORT dialect,
731 LPCSTR filename, DWORD access, DWORD sharing,
732 DWORD creation, DWORD attributes, USHORT *file_id )
734 unsigned char buffer[0x100];
735 int len = 0;
736 USHORT mode;
738 ERR("%s\n",filename);
740 mode = SMB_GetMode(access,sharing);
742 memset(buffer,0,sizeof buffer);
744 len = SMB_Header(buffer, SMB_COM_OPEN_ANDX, tree_id, user_id);
746 /* 0 */
747 buffer[len++] = 15; /* parameters */
748 buffer[len++] = 0xff; /* AndXCommand: secondary request */
749 buffer[len++] = 0x00; /* AndXReserved */
750 SMB_ADDWORD(buffer+len,0); len+=2; /* AndXOffset */
751 SMB_ADDWORD(buffer+len,0); len+=2; /* Flags */
752 SMB_ADDWORD(buffer+len,mode); len+=2; /* desired access */
753 SMB_ADDWORD(buffer+len,0); len+=2; /* search attributes */
754 SMB_ADDWORD(buffer+len,0); len+=2;
756 /*FIXME: complete */
757 return FALSE;
759 #endif
761 static BOOL SMB_Open(int fd, USHORT tree_id, USHORT user_id, USHORT dialect,
762 LPCSTR filename, DWORD access, DWORD sharing,
763 DWORD creation, DWORD attributes, USHORT *file_id )
765 unsigned char buffer[0x100];
766 int len = 0,slen,pcount,i;
767 USHORT mode = SMB_GetMode(access,sharing);
769 ERR("%s\n",filename);
771 memset(buffer,0,sizeof buffer);
773 len = SMB_Header(buffer, SMB_COM_OPEN, tree_id, user_id);
775 /* 0 */
776 buffer[len++] = 2; /* parameters */
777 SMB_ADDWORD(buffer+len,mode); len+=2;
778 SMB_ADDWORD(buffer+len,0); len+=2; /* search attributes */
780 slen = strlen(filename)+2; /* inc. nul and BufferFormat */
781 SMB_ADDWORD(buffer+len,slen); len+=2;
783 buffer[len] = 0x04; /* BufferFormat */
784 strcpy(&buffer[len+1],filename);
785 len += slen;
787 if(!NB_Transaction(fd, buffer, len, &len))
788 return FALSE;
790 if(SMB_GetError(buffer))
791 return FALSE;
793 pcount = SMB_PARAM_COUNT(buffer);
795 if( (SMB_HDRSIZE+pcount*2) > len )
797 ERR("Bad parameter count %d\n",pcount);
798 return FALSE;
801 ERR("response, %d args: ",pcount);
802 for(i=0; i<pcount; i++)
803 DPRINTF("%04x ",SMB_PARAM(buffer,i));
804 DPRINTF("\n");
806 *file_id = SMB_PARAM(buffer,0);
808 ERR("file_id = %04x\n",*file_id);
810 return TRUE;
813 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)
815 unsigned char *buffer;
816 int len,buf_size,n,i;
818 ERR("user %04x tree %04x file %04x count %04x offset %08lx\n",
819 user_id, tree_id, file_id, count, offset);
821 buf_size = count+0x100;
822 buffer = (unsigned char *) HeapAlloc(GetProcessHeap(),0,buf_size);
824 memset(buffer,0,buf_size);
826 len = SMB_Header(buffer, SMB_COM_READ, tree_id, user_id);
828 buffer[len++] = 5;
829 SMB_ADDWORD(&buffer[len],file_id); len += 2;
830 SMB_ADDWORD(&buffer[len],count); len += 2;
831 SMB_ADDDWORD(&buffer[len],offset); len += 4;
832 SMB_ADDWORD(&buffer[len],0); len += 2; /* how many more bytes will be read */
834 buffer[len++] = 0;
836 if(!NB_Transaction(fd, buffer, len, &len))
838 HeapFree(GetProcessHeap(),0,buffer);
839 return FALSE;
842 if(SMB_GetError(buffer))
844 HeapFree(GetProcessHeap(),0,buffer);
845 return FALSE;
848 n = SMB_PARAM_COUNT(buffer);
850 if( (SMB_HDRSIZE+n*2) > len )
852 HeapFree(GetProcessHeap(),0,buffer);
853 ERR("Bad parameter count %d\n",n);
854 return FALSE;
857 ERR("response, %d args: ",n);
858 for(i=0; i<n; i++)
859 DPRINTF("%04x ",SMB_PARAM(buffer,i));
860 DPRINTF("\n");
862 n = SMB_PARAM(buffer,5) - 3;
863 if(n>count)
864 n=count;
866 memcpy( out, &SMB_BUFFER(buffer,3), n);
868 ERR("Read %d bytes\n",n);
869 *read = n;
871 HeapFree(GetProcessHeap(),0,buffer);
873 return TRUE;
876 static int SMB_GetSocket(LPCSTR host)
878 int fd=-1,r;
879 struct sockaddr_in sin;
880 struct hostent *he;
882 ERR("host %s\n",host);
884 if(NB_Lookup(host,&sin))
885 goto connect;
887 he = gethostbyname(host);
888 if(he)
890 memcpy(&sin.sin_addr,he->h_addr, sizeof (sin.sin_addr));
891 goto connect;
894 /* FIXME: resolve by WINS too */
896 ERR("couldn't resolve SMB host %s\n", host);
898 return -1;
900 connect:
901 sin.sin_family = AF_INET;
902 sin.sin_port = htons(139); /* netbios session */
904 fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
905 if(fd<0)
906 return fd;
909 unsigned char *x = (unsigned char *)&sin.sin_addr;
910 ERR("Connecting to %d.%d.%d.%d ...\n", x[0],x[1],x[2],x[3]);
912 r = connect(fd, &sin, sizeof sin);
914 if(!NB_SessionReq(fd, "*SMBSERVER", "WINE"))
916 close(fd);
917 return -1;
920 return fd;
923 static BOOL SMB_LoginAndConnect(int fd, LPCSTR host, LPCSTR share, USHORT *tree_id, USHORT *user_id, USHORT *dialect)
925 LPSTR name=NULL;
927 ERR("host %s share %s\n",host,share);
929 if(!SMB_NegotiateProtocol(fd, dialect))
930 return FALSE;
932 if(!SMB_SessionSetup(fd, user_id))
933 return FALSE;
935 name = HeapAlloc(GetProcessHeap(),0,strlen(host)+strlen(share)+5);
936 if(!name)
937 return FALSE;
939 sprintf(name,"\\\\%s\\%s",host,share);
940 if(!SMB_TreeConnect(fd,*user_id,name,tree_id))
942 HeapFree(GetProcessHeap(),0,name);
943 return FALSE;
946 return TRUE;
949 static HANDLE SMB_RegisterFile( int fd, USHORT tree_id, USHORT user_id, USHORT dialect, USHORT file_id)
951 int r;
952 HANDLE ret;
954 wine_server_send_fd( fd );
956 SERVER_START_REQ( create_smb )
958 req->tree_id = tree_id;
959 req->user_id = user_id;
960 req->file_id = file_id;
961 req->dialect = 0;
962 req->fd = fd;
963 SetLastError(0);
964 r = wine_server_call_err( req );
965 ret = reply->handle;
967 SERVER_END_REQ;
969 if(!r)
970 ERR("created wineserver smb object, handle = %04x\n",ret);
971 else
972 SetLastError( ERROR_PATH_NOT_FOUND );
974 return ret;
977 HANDLE WINAPI SMB_CreateFileA( LPCSTR uncname, DWORD access, DWORD sharing,
978 LPSECURITY_ATTRIBUTES sa, DWORD creation,
979 DWORD attributes, HANDLE template )
981 int fd;
982 USHORT tree_id=0, user_id=0, dialect=0, file_id=0;
983 LPSTR name,host,share,file;
984 HANDLE handle = INVALID_HANDLE_VALUE;
986 name = HeapAlloc(GetProcessHeap(),0,lstrlenA(uncname));
987 if(!name)
988 return handle;
990 lstrcpyA(name,uncname);
992 if( !UNC_SplitName(name, &host, &share, &file) )
994 HeapFree(GetProcessHeap(),0,name);
995 return handle;
998 ERR("server is %s, share is %s, file is %s\n", host, share, file);
1000 fd = SMB_GetSocket(host);
1001 if(fd < 0)
1002 goto done;
1004 if(!SMB_LoginAndConnect(fd, host, share, &tree_id, &user_id, &dialect))
1005 goto done;
1007 #if 0
1008 if(!SMB_NtCreateOpen(fd, tree_id, user_id, dialect, file,
1009 access, sharing, sa, creation, attributes, template, &file_id ))
1011 close(fd);
1012 ERR("CreateOpen failed\n");
1013 goto done;
1015 #endif
1016 if(!SMB_Open(fd, tree_id, user_id, dialect, file,
1017 access, sharing, creation, attributes, &file_id ))
1019 close(fd);
1020 ERR("CreateOpen failed\n");
1021 goto done;
1024 handle = SMB_RegisterFile(fd, tree_id, user_id, dialect, file_id);
1025 if(!handle)
1027 ERR("register failed\n");
1028 close(fd);
1031 done:
1032 HeapFree(GetProcessHeap(),0,name);
1033 return handle;
1036 static BOOL SMB_GetSmbInfo(HANDLE hFile, USHORT *tree_id, USHORT *user_id, USHORT *dialect, USHORT *file_id, LPDWORD offset)
1038 int r;
1040 SERVER_START_REQ( get_smb_info )
1042 req->handle = hFile;
1043 req->flags = 0;
1044 SetLastError(0);
1045 r = wine_server_call_err( req );
1046 if(tree_id)
1047 *tree_id = reply->tree_id;
1048 if(user_id)
1049 *user_id = reply->user_id;
1050 if(file_id)
1051 *file_id = reply->file_id;
1052 if(dialect)
1053 *dialect = reply->dialect;
1054 if(offset)
1055 *offset = reply->offset;
1057 SERVER_END_REQ;
1059 return !r;
1062 static BOOL SMB_SetOffset(HANDLE hFile, DWORD offset)
1064 int r;
1066 ERR("offset = %08lx\n",offset);
1068 SERVER_START_REQ( get_smb_info )
1070 req->handle = hFile;
1071 req->flags = SMBINFO_SET_OFFSET;
1072 req->offset = offset;
1073 SetLastError(0);
1074 r = wine_server_call_err( req );
1075 /* if(offset)
1076 *offset = reply->offset; */
1078 SERVER_END_REQ;
1080 return !r;
1083 BOOL WINAPI SMB_ReadFile(HANDLE hFile, LPVOID buffer, DWORD bytesToRead, LPDWORD bytesRead, LPOVERLAPPED lpOverlapped)
1085 int fd;
1086 DWORD total, count, offset;
1087 USHORT user_id, tree_id, dialect, file_id, read;
1088 BOOL r=TRUE;
1090 ERR("%04x %p %ld %p\n", hFile, buffer, bytesToRead, bytesRead);
1092 if(!SMB_GetSmbInfo(hFile, &tree_id, &user_id, &dialect, &file_id, &offset))
1093 return FALSE;
1095 fd = FILE_GetUnixHandle(hFile, GENERIC_READ);
1096 if(fd<0)
1097 return FALSE;
1099 total = 0;
1100 while(1)
1102 count = bytesToRead - total;
1103 if(count>0x400)
1104 count = 0x400;
1105 if(count==0)
1106 break;
1107 read = 0;
1108 r = SMB_Read(fd, tree_id, user_id, dialect, file_id, offset, buffer, count, &read);
1109 if(!r)
1110 break;
1111 if(!read)
1112 break;
1113 total += read;
1114 buffer = (char*)buffer + read;
1115 offset += read;
1116 if(total>=bytesToRead)
1117 break;
1119 close(fd);
1121 if(bytesRead)
1122 *bytesRead = total;
1124 if(!SMB_SetOffset(hFile, offset))
1125 return FALSE;
1127 return r;