Check if 'GLX_ARB_get_proc_address' is supported before using
[wine/multimedia.git] / files / smb.c
blobee7ed18b447a54982d62d8e1d3dce79706c953f8
1 /*
2 * Copyright (C) 2002 Mike McCormack
4 * CIFS implementation for WINE
6 * This is a WINE's implementation of the Common Internet File System
8 * for specification see:
10 * http://www.codefx.com/CIFS_Explained.htm
11 * http://www.ubiqx.org/cifs/rfc-draft/rfc1002.html
12 * http://www.ubiqx.org/cifs/rfc-draft/draft-leach-cifs-v1-spec-02.html
13 * http://ubiqx.org/cifs/
14 * http://www.samba.org
16 * This library is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU Lesser General Public
18 * License as published by the Free Software Foundation; either
19 * version 2.1 of the License, or (at your option) any later version.
21 * This library is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * Lesser General Public License for more details.
26 * You should have received a copy of the GNU Lesser General Public
27 * License along with this library; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 * FIXME:
33 * - There is a race condition when two threads try to read from the same
34 * SMB handle. Either we need to lock the SMB handle for the time we
35 * use it in the client, or do all reading and writing to the socket
36 * fd in the server.
38 * - Each new handle opens up a new connection to the SMB server. This
39 * is not ideal, since operations can be multiplexed on one socket. For
40 * this to work properly we would need to have some way of discovering
41 * connections that are already open.
43 * - All access is currently anonymous. Password protected shares cannot
44 * be accessed. We need some way of organising passwords, storing them
45 * in the config file, or putting up a dialog box for the user.
47 * - We don't deal with SMB dialects at all.
49 * - SMB supports passing unicode over the wire, should use this if possible.
51 * - Implement ability to read named pipes over the network. Would require
52 * integrate this code with the named pipes code in the server, and
53 * possibly implementing some support for security tokens.
56 #include "config.h"
57 #include "wine/port.h"
59 #include <assert.h>
60 #include <ctype.h>
61 #include <fcntl.h>
62 #include <stdlib.h>
63 #include <stdio.h>
64 #include <string.h>
65 #include <sys/types.h>
66 #include <sys/stat.h>
67 #ifdef HAVE_SYS_MMAN_H
68 #include <sys/mman.h>
69 #endif
70 #ifdef HAVE_SYS_TIME_H
71 # include <sys/time.h>
72 #endif
73 #ifdef HAVE_SYS_POLL_H
74 # include <sys/poll.h>
75 #endif
76 #include <time.h>
77 #ifdef HAVE_UNISTD_H
78 # include <unistd.h>
79 #endif
80 #ifdef HAVE_UTIME_H
81 # include <utime.h>
82 #endif
83 #ifdef HAVE_SYS_SOCKET_H
84 # include <sys/socket.h>
85 #endif
86 #include <sys/types.h>
87 #ifdef HAVE_NETINET_IN_SYSTM_H
88 #include <netinet/in_systm.h>
89 #endif
90 #ifdef HAVE_NETINET_IN_H
91 #include <netinet/in.h>
92 #endif
93 #ifdef HAVE_NETINET_IP_H
94 #include <netinet/ip.h>
95 #endif
96 #ifdef HAVE_ARPA_INET_H
97 #include <arpa/inet.h>
98 #endif
99 #ifdef HAVE_NETDB_H
100 #include <netdb.h>
101 #endif
103 #define NONAMELESSUNION
104 #define NONAMELESSSTRUCT
105 #include "winerror.h"
106 #include "windef.h"
107 #include "winbase.h"
108 #include "winnls.h"
109 #include "file.h"
111 #include "smb.h"
112 #include "winternl.h"
113 #include "ntdll_misc.h"
115 #include "wine/server.h"
116 #include "wine/debug.h"
118 WINE_DEFAULT_DEBUG_CHANNEL(file);
120 #define NBR_ADDWORD(p,word) { (p)[1] = (word & 0xff); (p)[0] = ((word)>>8)&0xff; }
121 #define NBR_GETWORD(p) ( (((p)[0])<<8) | ((p)[1]) )
123 #define SMB_ADDWORD(p,word) { (p)[0] = (word & 0xff); (p)[1] = ((word)>>8)&0xff; }
124 #define SMB_GETWORD(p) ( (((p)[1])<<8) | ((p)[0]) )
125 #define SMB_ADDDWORD(p,w) { (p)[3]=((w)>>24)&0xff; (p)[2]=((w)>>16)&0xff; (p)[1]=((w)>>8)&0xff; (p)[0]=(w)&0xff; }
126 #define SMB_GETDWORD(p) ( (((p)[3])<<24) | (((p)[2])<<16) | (((p)[1])<<8) | ((p)[0]) )
128 #define SMB_COM_CREATE_DIRECTORY 0x00
129 #define SMB_COM_DELETE_DIRECTORY 0x01
130 #define SMB_COM_OPEN 0x02
131 #define SMB_COM_CREATE 0x03
132 #define SMB_COM_CLOSE 0x04
133 #define SMB_COM_FLUSH 0x05
134 #define SMB_COM_DELETE 0x06
135 #define SMB_COM_RENAME 0x07
136 #define SMB_COM_QUERY_INFORMATION 0x08
137 #define SMB_COM_SET_INFORMATION 0x09
138 #define SMB_COM_READ 0x0A
139 #define SMB_COM_WRITE 0x0B
140 #define SMB_COM_LOCK_BYTE_RANGE 0x0C
141 #define SMB_COM_UNLOCK_BYTE_RANGE 0x0D
142 #define SMB_COM_CREATE_TEMPORARY 0x0E
143 #define SMB_COM_CREATE_NEW 0x0F
144 #define SMB_COM_CHECK_DIRECTORY 0x10
145 #define SMB_COM_PROCESS_EXIT 0x11
146 #define SMB_COM_SEEK 0x12
147 #define SMB_COM_LOCK_AND_READ 0x13
148 #define SMB_COM_WRITE_AND_UNLOCK 0x14
149 #define SMB_COM_READ_RAW 0x1A
150 #define SMB_COM_READ_MPX 0x1B
151 #define SMB_COM_READ_MPX_SECONDARY 0x1C
152 #define SMB_COM_WRITE_RAW 0x1D
153 #define SMB_COM_WRITE_MPX 0x1E
154 #define SMB_COM_WRITE_COMPLETE 0x20
155 #define SMB_COM_SET_INFORMATION2 0x22
156 #define SMB_COM_QUERY_INFORMATION2 0x23
157 #define SMB_COM_LOCKING_ANDX 0x24
158 #define SMB_COM_TRANSACTION 0x25
159 #define SMB_COM_TRANSACTION_SECONDARY 0x26
160 #define SMB_COM_IOCTL 0x27
161 #define SMB_COM_IOCTL_SECONDARY 0x28
162 #define SMB_COM_COPY 0x29
163 #define SMB_COM_MOVE 0x2A
164 #define SMB_COM_ECHO 0x2B
165 #define SMB_COM_WRITE_AND_CLOSE 0x2C
166 #define SMB_COM_OPEN_ANDX 0x2D
167 #define SMB_COM_READ_ANDX 0x2E
168 #define SMB_COM_WRITE_ANDX 0x2F
169 #define SMB_COM_CLOSE_AND_TREE_DISC 0x31
170 #define SMB_COM_TRANSACTION2 0x32
171 #define SMB_COM_TRANSACTION2_SECONDARY 0x33
172 #define SMB_COM_FIND_CLOSE2 0x34
173 #define SMB_COM_FIND_NOTIFY_CLOSE 0x35
174 #define SMB_COM_TREE_CONNECT 0x70
175 #define SMB_COM_TREE_DISCONNECT 0x71
176 #define SMB_COM_NEGOTIATE 0x72
177 #define SMB_COM_SESSION_SETUP_ANDX 0x73
178 #define SMB_COM_LOGOFF_ANDX 0x74
179 #define SMB_COM_TREE_CONNECT_ANDX 0x75
180 #define SMB_COM_QUERY_INFORMATION_DISK 0x80
181 #define SMB_COM_SEARCH 0x81
182 #define SMB_COM_FIND 0x82
183 #define SMB_COM_FIND_UNIQUE 0x83
184 #define SMB_COM_NT_TRANSACT 0xA0
185 #define SMB_COM_NT_TRANSACT_SECONDARY 0xA1
186 #define SMB_COM_NT_CREATE_ANDX 0xA2
187 #define SMB_COM_NT_CANCEL 0xA4
188 #define SMB_COM_OPEN_PRINT_FILE 0xC0
189 #define SMB_COM_WRITE_PRINT_FILE 0xC1
190 #define SMB_COM_CLOSE_PRINT_FILE 0xC2
191 #define SMB_COM_GET_PRINT_QUEUE 0xC3
193 #define TRANS2_FIND_FIRST2 0x01
194 #define TRANS2_FIND_NEXT2 0x02
196 #define MAX_HOST_NAME 15
197 #define NB_TIMEOUT 10000
199 /* We only need the A versions locally currently */
200 static inline int SMB_isSepA (CHAR c) {return (c == '\\' || c == '/');}
201 static inline int SMB_isUNCA (LPCSTR filename) {return (filename && SMB_isSepW (filename[0]) && SMB_isSepW (filename[1]));}
202 static inline CHAR *SMB_nextSepA (CHAR *s) {while (*s && !SMB_isSepA (*s)) s++; return (*s? s : 0);}
203 /* NB SM_nextSepA cannot return const CHAR * since it is going to be used for
204 * replacing separators with null characters
207 static USHORT SMB_MultiplexId = 0;
209 struct NB_Buffer
211 unsigned char *buffer;
212 int len;
215 static int netbios_name(const char *p, unsigned char *buffer)
217 char ch;
218 int i,len=0;
220 buffer[len++]=' ';
221 for(i=0; i<=MAX_HOST_NAME; i++)
223 if(i<MAX_HOST_NAME)
225 if(*p)
226 ch = *p++&0xdf; /* add character from hostname */
227 else
228 ch = ' '; /* add padding */
230 else
231 ch = 0; /* add terminator */
232 buffer[len++] = ((ch&0xf0) >> 4) + 'A';
233 buffer[len++] = (ch&0x0f) + 'A';
235 buffer[len++] = 0; /* add second terminator */
236 return len;
239 static DWORD NB_NameReq(LPCSTR host, unsigned char *buffer, int len)
241 int trn = 1234,i=0;
243 NBR_ADDWORD(&buffer[i],trn); i+=2;
244 NBR_ADDWORD(&buffer[i],0x0110); i+=2;
245 NBR_ADDWORD(&buffer[i],0x0001); i+=2;
246 NBR_ADDWORD(&buffer[i],0x0000); i+=2;
247 NBR_ADDWORD(&buffer[i],0x0000); i+=2;
248 NBR_ADDWORD(&buffer[i],0x0000); i+=2;
250 i += netbios_name(host,&buffer[i]);
252 NBR_ADDWORD(&buffer[i],0x0020); i+=2;
253 NBR_ADDWORD(&buffer[i],0x0001); i+=2;
255 TRACE("packet is %d bytes in length\n",i);
258 int j;
259 for(j=0; j<i; j++)
260 printf("%02x%c",buffer[j],(((j+1)%16)&&((j+1)!=j))?' ':'\n');
263 return i;
266 /* unc = \\hostname\share\file... */
267 static BOOL UNC_SplitName(LPSTR unc, LPSTR *hostname, LPSTR *share, LPSTR *file)
269 char *p;
271 TRACE("%s\n",unc);
273 if (!SMB_isUNCA (unc))
274 return FALSE;
275 p = unc + 2;
276 *hostname=p;
278 p = SMB_nextSepA (p);
279 if(!p)
280 return FALSE;
281 *p=0;
282 *share = ++p;
284 p = SMB_nextSepA (p);
285 if(!p)
286 return FALSE;
287 *p=0;
288 *file = ++p;
290 return TRUE;
293 static BOOL NB_Lookup(LPCSTR host, struct sockaddr_in *addr)
295 int fd,on=1,r,len,i,fromsize;
296 struct pollfd fds;
297 struct sockaddr_in sin,fromaddr;
298 unsigned char buffer[256];
300 fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
301 if(fd<0)
302 return FALSE;
304 r = setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));
305 if(r<0)
306 goto err;
308 if(0==inet_aton("255.255.255.255", (struct in_addr *)&sin.sin_addr.s_addr))
310 FIXME("Error getting bcast address\n");
311 goto err;
313 sin.sin_family = AF_INET;
314 sin.sin_port = htons(137);
316 len = NB_NameReq(host,buffer,sizeof(buffer));
317 if(len<=0)
318 goto err;
320 r = sendto(fd, buffer, len, 0, (struct sockaddr*)&sin, sizeof(sin));
321 if(r<0)
323 FIXME("Error sending packet\n");
324 goto err;
327 fds.fd = fd;
328 fds.events = POLLIN;
329 fds.revents = 0;
331 /* FIXME: this is simple and easily fooled logic
332 * we should loop until we receive the correct packet or timeout
334 r = poll(&fds,1,NB_TIMEOUT);
335 if(r!=1)
336 goto err;
338 TRACE("Got response!\n");
340 fromsize = sizeof (fromaddr);
341 r = recvfrom(fd, buffer, sizeof(buffer), 0, (struct sockaddr*)&fromaddr, &fromsize);
342 if(r<0)
343 goto err;
345 TRACE("%d bytes received\n",r);
347 if(r!=62)
348 goto err;
350 for(i=0; i<r; i++)
351 DPRINTF("%02X%c",buffer[i],(((i+1)!=r)&&((i+1)%16))?' ':'\n');
352 DPRINTF("\n");
354 if(0x0f & buffer[3])
355 goto err;
357 TRACE("packet is OK\n");
359 memcpy(&addr->sin_addr, &buffer[58], sizeof(addr->sin_addr));
361 close(fd);
362 return TRUE;
364 err:
365 close(fd);
366 return FALSE;
369 #define NB_FIRST 0x40
371 #define NB_HDRSIZE 4
373 #define NB_SESSION_MSG 0x00
374 #define NB_SESSION_REQ 0x81
376 /* RFC 1002, section 4.3.2 */
377 static BOOL NB_SessionReq(int fd, char *called, char *calling)
379 unsigned char buffer[0x100];
380 int len = 0,r;
381 struct pollfd fds;
383 TRACE("called %s, calling %s\n",called,calling);
385 buffer[0] = NB_SESSION_REQ;
386 buffer[1] = NB_FIRST;
388 netbios_name(called, &buffer[NB_HDRSIZE]);
389 len += 34;
390 netbios_name(calling, &buffer[NB_HDRSIZE+len]);
391 len += 34;
393 NBR_ADDWORD(&buffer[2],len);
395 /* for(i=0; i<(len+NB_HDRSIZE); i++)
396 DPRINTF("%02X%c",buffer[i],(((i+1)!=(len+4))&&((i+1)%16))?' ':'\n'); */
398 r = write(fd,buffer,len+4);
399 if(r<0)
401 ERR("Write failed\n");
402 return FALSE;
405 fds.fd = fd;
406 fds.events = POLLIN;
407 fds.revents = 0;
409 r = poll(&fds,1,NB_TIMEOUT);
410 if(r!=1)
412 ERR("Poll failed\n");
413 return FALSE;
416 r = read(fd, buffer, NB_HDRSIZE);
417 if((r!=NB_HDRSIZE) || (buffer[0]!=0x82))
419 TRACE("Received %d bytes\n",r);
420 TRACE("%02x %02x %02x %02x\n", buffer[0],buffer[1],buffer[2],buffer[3]);
421 return FALSE;
424 return TRUE;
427 static BOOL NB_SendData(int fd, struct NB_Buffer *out)
429 unsigned char buffer[NB_HDRSIZE];
430 int r;
432 /* CHECK: is it always OK to do this in two writes? */
433 /* perhaps use scatter gather sendmsg instead? */
435 buffer[0] = NB_SESSION_MSG;
436 buffer[1] = NB_FIRST;
437 NBR_ADDWORD(&buffer[2],out->len);
439 r = write(fd, buffer, NB_HDRSIZE);
440 if(r!=NB_HDRSIZE)
441 return FALSE;
443 r = write(fd, out->buffer, out->len);
444 if(r!=out->len)
446 ERR("write failed\n");
447 return FALSE;
450 return TRUE;
453 static BOOL NB_RecvData(int fd, struct NB_Buffer *rx)
455 int r;
456 unsigned char buffer[NB_HDRSIZE];
458 r = read(fd, buffer, NB_HDRSIZE);
459 if((r!=NB_HDRSIZE) || (buffer[0]!=NB_SESSION_MSG))
461 ERR("Received %d bytes\n",r);
462 return FALSE;
465 rx->len = NBR_GETWORD(&buffer[2]);
467 rx->buffer = RtlAllocateHeap(ntdll_get_process_heap(), 0, rx->len);
468 if(!rx->buffer)
469 return FALSE;
471 r = read(fd, rx->buffer, rx->len);
472 if(rx->len!=r)
474 TRACE("Received %d bytes\n",r);
475 RtlFreeHeap(ntdll_get_process_heap(), 0, rx->buffer);
476 rx->buffer = 0;
477 rx->len = 0;
478 return FALSE;
481 return TRUE;
484 static BOOL NB_Transaction(int fd, struct NB_Buffer *in, struct NB_Buffer *out)
486 int r;
487 struct pollfd fds;
489 if(TRACE_ON(file))
491 int i;
492 DPRINTF("Sending request:\n");
493 for(i=0; i<in->len; i++)
494 DPRINTF("%02X%c",in->buffer[i],(((i+1)!=in->len)&&((i+1)%16))?' ':'\n');
497 if(!NB_SendData(fd,in))
498 return FALSE;
500 fds.fd = fd;
501 fds.events = POLLIN;
502 fds.revents = 0;
504 r = poll(&fds,1,NB_TIMEOUT);
505 if(r!=1)
507 ERR("Poll failed\n");
508 return FALSE;
511 if(!NB_RecvData(fd, out))
512 return FALSE;
514 if(TRACE_ON(file))
516 int i;
517 DPRINTF("Got response:\n");
518 for(i=0; i<out->len; i++)
519 DPRINTF("%02X%c",out->buffer[i],(((i+1)!=out->len)&&((i+1)%16))?' ':'\n');
522 return TRUE;
525 #define SMB_ADDHEADER(b,l) { b[(l)++]=0xff; b[(l)++]='S'; b[(l)++]='M'; b[(l)++]='B'; }
526 #define SMB_ADDERRINFO(b,l) { b[(l)++]=0; b[(l)++]=0; b[(l)++]=0; b[(l)++]=0; }
527 #define SMB_ADDPADSIG(b,l) { memset(&b[l],0,12); l+=12; }
529 #define SMB_ERRCLASS 5
530 #define SMB_ERRCODE 7
531 #define SMB_TREEID 24
532 #define SMB_PROCID 26
533 #define SMB_USERID 28
534 #define SMB_PLEXID 30
535 #define SMB_PCOUNT 32
536 #define SMB_HDRSIZE 33
538 static DWORD SMB_GetError(unsigned char *buffer)
540 char *err_class;
542 switch(buffer[SMB_ERRCLASS])
544 case 0:
545 return STATUS_SUCCESS;
546 case 1:
547 err_class = "DOS";
548 break;
549 case 2:
550 err_class = "net server";
551 break;
552 case 3:
553 err_class = "hardware";
554 break;
555 case 0xff:
556 err_class = "smb";
557 break;
558 default:
559 err_class = "unknown";
560 break;
563 ERR("%s error %d \n",err_class, buffer[SMB_ERRCODE]);
565 /* FIXME: return propper error codes */
566 return STATUS_INVALID_PARAMETER;
569 static int SMB_Header(unsigned char *buffer, unsigned char command, USHORT tree_id, USHORT user_id)
571 int len = 0;
572 DWORD id;
574 /* 0 */
575 SMB_ADDHEADER(buffer,len);
577 /* 4 */
578 buffer[len++] = command;
580 /* 5 */
581 SMB_ADDERRINFO(buffer,len)
583 /* 9 */
584 buffer[len++] = 0x00; /* flags */
585 SMB_ADDWORD(&buffer[len],1); len += 2; /* flags2 */
587 /* 12 */
588 SMB_ADDPADSIG(buffer,len)
590 /* 24 */
591 SMB_ADDWORD(&buffer[len],tree_id); len += 2; /* treeid */
592 id = GetCurrentThreadId();
593 SMB_ADDWORD(&buffer[len],id); len += 2; /* process id */
594 SMB_ADDWORD(&buffer[len],user_id); len += 2; /* user id */
595 SMB_ADDWORD(&buffer[len],SMB_MultiplexId); len += 2; /* multiplex id */
596 SMB_MultiplexId++;
598 return len;
601 static const char *SMB_ProtocolDialect = "NT LM 0.12";
602 /* = "Windows for Workgroups 3.1a"; */
604 /* FIXME: support multiple SMB dialects */
605 static BOOL SMB_NegotiateProtocol(int fd, USHORT *dialect)
607 unsigned char buf[0x100];
608 int buflen = 0;
609 struct NB_Buffer tx, rx;
611 TRACE("\n");
613 memset(buf,0,sizeof(buf));
615 tx.buffer = buf;
616 tx.len = SMB_Header(tx.buffer, SMB_COM_NEGOTIATE, 0, 0);
618 /* parameters */
619 tx.buffer[tx.len++] = 0; /* no parameters */
621 /* command buffer */
622 buflen = strlen(SMB_ProtocolDialect)+2; /* include type and nul byte */
623 SMB_ADDWORD(&tx.buffer[tx.len],buflen); tx.len += 2;
625 tx.buffer[tx.len] = 0x02;
626 strcpy(&tx.buffer[tx.len+1],SMB_ProtocolDialect);
627 tx.len += buflen;
629 rx.buffer = NULL;
630 rx.len = 0;
631 if(!NB_Transaction(fd, &tx, &rx))
633 ERR("Failed\n");
634 return FALSE;
637 if(!rx.buffer)
638 return FALSE;
640 /* FIXME: check response */
641 if(SMB_GetError(rx.buffer))
643 ERR("returned error\n");
644 RtlFreeHeap(ntdll_get_process_heap(),0,rx.buffer);
645 return FALSE;
648 RtlFreeHeap(ntdll_get_process_heap(),0,rx.buffer);
650 *dialect = 0;
652 return TRUE;
655 #define SMB_PARAM_COUNT(buffer) ((buffer)[SMB_PCOUNT])
656 #define SMB_PARAM(buffer,n) SMB_GETWORD(&(buffer)[SMB_HDRSIZE+2*(n)])
657 #define SMB_BUFFER_COUNT(buffer) SMB_GETWORD(buffer+SMB_HDRSIZE+2*SMB_PARAM_COUNT(buffer))
658 #define SMB_BUFFER(buffer,n) ((buffer)[SMB_HDRSIZE + 2*SMB_PARAM_COUNT(buffer) + 2 + (n) ])
660 static BOOL SMB_SessionSetup(int fd, USHORT *userid)
662 unsigned char buf[0x100];
663 int pcount,bcount;
664 struct NB_Buffer rx, tx;
666 memset(buf,0,sizeof(buf));
667 tx.buffer = buf;
669 tx.len = SMB_Header(tx.buffer, SMB_COM_SESSION_SETUP_ANDX, 0, 0);
671 tx.buffer[tx.len++] = 0; /* no parameters? */
673 tx.buffer[tx.len++] = 0xff; /* AndXCommand: secondary request */
674 tx.buffer[tx.len++] = 0x00; /* AndXReserved */
675 SMB_ADDWORD(&tx.buffer[tx.len],0); /* AndXOffset */
676 tx.len += 2;
677 SMB_ADDWORD(&tx.buffer[tx.len],0x400); /* MaxBufferSize */
678 tx.len += 2;
679 SMB_ADDWORD(&tx.buffer[tx.len],1); /* MaxMpxCount */
680 tx.len += 2;
681 SMB_ADDWORD(&tx.buffer[tx.len],0); /* VcNumber */
682 tx.len += 2;
683 SMB_ADDWORD(&tx.buffer[tx.len],0); /* SessionKey */
684 tx.len += 2;
685 SMB_ADDWORD(&tx.buffer[tx.len],0); /* SessionKey */
686 tx.len += 2;
687 SMB_ADDWORD(&tx.buffer[tx.len],0); /* Password length */
688 tx.len += 2;
689 SMB_ADDWORD(&tx.buffer[tx.len],0); /* Reserved */
690 tx.len += 2;
691 SMB_ADDWORD(&tx.buffer[tx.len],0); /* Reserved */
692 tx.len += 2;
694 /* FIXME: add name and password here */
695 tx.buffer[tx.len++] = 0; /* number of bytes in password */
697 rx.buffer = NULL;
698 rx.len = 0;
699 if(!NB_Transaction(fd, &tx, &rx))
700 return FALSE;
702 if(!rx.buffer)
703 return FALSE;
705 if(SMB_GetError(rx.buffer))
706 goto done;
708 pcount = SMB_PARAM_COUNT(rx.buffer);
710 if( (SMB_HDRSIZE+pcount*2) > rx.len )
712 ERR("Bad parameter count %d\n",pcount);
713 goto done;
716 if(TRACE_ON(file))
718 int i;
719 DPRINTF("SMB_COM_SESSION_SETUP response, %d args: ",pcount);
720 for(i=0; i<pcount; i++)
721 DPRINTF("%04x ",SMB_PARAM(rx.buffer,i));
722 DPRINTF("\n");
725 bcount = SMB_BUFFER_COUNT(rx.buffer);
726 if( (SMB_HDRSIZE+pcount*2+2+bcount) > rx.len )
728 ERR("parameter count %x, buffer count %x, len %x\n",pcount,bcount,rx.len);
729 goto done;
732 if(TRACE_ON(file))
734 int i;
735 DPRINTF("response buffer %d bytes: ",bcount);
736 for(i=0; i<bcount; i++)
738 unsigned char ch = SMB_BUFFER(rx.buffer,i);
739 DPRINTF("%c", isprint(ch)?ch:' ');
741 DPRINTF("\n");
744 *userid = SMB_GETWORD(&rx.buffer[SMB_USERID]);
746 RtlFreeHeap(ntdll_get_process_heap(),0,rx.buffer);
747 return TRUE;
749 done:
750 RtlFreeHeap(ntdll_get_process_heap(),0,rx.buffer);
751 return FALSE;
755 static BOOL SMB_TreeConnect(int fd, USHORT user_id, LPCSTR share_name, USHORT *treeid)
757 unsigned char buf[0x100];
758 int slen;
759 struct NB_Buffer rx,tx;
761 TRACE("%s\n",share_name);
763 memset(buf,0,sizeof(buf));
764 tx.buffer = buf;
766 tx.len = SMB_Header(tx.buffer, SMB_COM_TREE_CONNECT, 0, user_id);
768 tx.buffer[tx.len++] = 4; /* parameters */
770 tx.buffer[tx.len++] = 0xff; /* AndXCommand: secondary request */
771 tx.buffer[tx.len++] = 0x00; /* AndXReserved */
772 SMB_ADDWORD(&tx.buffer[tx.len],0); /* AndXOffset */
773 tx.len += 2;
774 SMB_ADDWORD(&tx.buffer[tx.len],0); /* Flags */
775 tx.len += 2;
776 SMB_ADDWORD(&tx.buffer[tx.len],1); /* Password length */
777 tx.len += 2;
779 /* SMB command buffer */
780 SMB_ADDWORD(&tx.buffer[tx.len],3); /* command buffer len */
781 tx.len += 2;
782 tx.buffer[tx.len++] = 0; /* null terminated password */
784 slen = strlen(share_name);
785 if(slen<(sizeof(buf)-tx.len))
786 strcpy(&tx.buffer[tx.len], share_name);
787 else
788 return FALSE;
789 tx.len += slen+1;
791 /* name of the service */
792 tx.buffer[tx.len++] = 0;
794 rx.buffer = NULL;
795 rx.len = 0;
796 if(!NB_Transaction(fd, &tx, &rx))
797 return FALSE;
799 if(!rx.buffer)
800 return FALSE;
802 if(SMB_GetError(rx.buffer))
804 RtlFreeHeap(ntdll_get_process_heap(),0,rx.buffer);
805 return FALSE;
808 *treeid = SMB_GETWORD(&rx.buffer[SMB_TREEID]);
810 RtlFreeHeap(ntdll_get_process_heap(),0,rx.buffer);
811 TRACE("OK, treeid = %04x\n", *treeid);
813 return TRUE;
816 #if 0 /* not yet */
817 static BOOL SMB_NtCreateOpen(int fd, USHORT tree_id, USHORT user_id, USHORT dialect,
818 LPCSTR filename, DWORD access, DWORD sharing,
819 LPSECURITY_ATTRIBUTES sa, DWORD creation,
820 DWORD attributes, HANDLE template, USHORT *file_id )
822 unsigned char buffer[0x100];
823 int len = 0,slen;
825 TRACE("%s\n",filename);
827 memset(buffer,0,sizeof(buffer));
829 len = SMB_Header(buffer, SMB_COM_NT_CREATE_ANDX, tree_id, user_id);
831 /* 0 */
832 buffer[len++] = 24; /* parameters */
834 buffer[len++] = 0xff; /* AndXCommand: secondary request */
835 buffer[len++] = 0x00; /* AndXReserved */
836 SMB_ADDWORD(&buffer[len],0); len += 2; /* AndXOffset */
838 buffer[len++] = 0; /* reserved */
839 slen = strlen(filename);
840 SMB_ADDWORD(&buffer[len],slen); len += 2; /* name length */
842 /* 0x08 */
843 SMB_ADDDWORD(&buffer[len],0); len += 4; /* flags */
844 SMB_ADDDWORD(&buffer[len],0); len += 4; /* root directory fid */
845 /* 0x10 */
846 SMB_ADDDWORD(&buffer[len],access); len += 4; /* access */
847 SMB_ADDDWORD(&buffer[len],0); len += 4; /* allocation size */
848 /* 0x18 */
849 SMB_ADDDWORD(&buffer[len],0); len += 4; /* root directory fid */
851 /* 0x1c */
852 SMB_ADDDWORD(&buffer[len],0); len += 4; /* initial allocation */
853 SMB_ADDDWORD(&buffer[len],0); len += 4;
855 /* 0x24 */
856 SMB_ADDDWORD(&buffer[len],attributes); len += 4; /* ExtFileAttributes*/
858 /* 0x28 */
859 SMB_ADDDWORD(&buffer[len],sharing); len += 4; /* ShareAccess */
861 /* 0x2c */
862 TRACE("creation = %08lx\n",creation);
863 SMB_ADDDWORD(&buffer[len],creation); len += 4; /* CreateDisposition */
865 /* 0x30 */
866 SMB_ADDDWORD(&buffer[len],creation); len += 4; /* CreateOptions */
868 /* 0x34 */
869 SMB_ADDDWORD(&buffer[len],0); len += 4; /* Impersonation */
871 /* 0x38 */
872 buffer[len++] = 0; /* security flags */
874 /* 0x39 */
875 SMB_ADDWORD(&buffer[len],slen); len += 2; /* size of buffer */
877 if(slen<(sizeof(buffer)-len))
878 strcpy(&buffer[len], filename);
879 else
880 return FALSE;
881 len += slen+1;
883 /* name of the file */
884 buffer[len++] = 0;
886 if(!NB_Transaction(fd, buffer, len, &len))
887 return FALSE;
889 if(SMB_GetError(buffer))
890 return FALSE;
892 TRACE("OK\n");
894 /* FIXME */
895 /* *file_id = SMB_GETWORD(&buffer[xxx]); */
896 *file_id = 0;
897 return FALSE;
899 return TRUE;
901 #endif
903 static USHORT SMB_GetMode(DWORD access, DWORD sharing)
905 USHORT mode=0;
907 switch(access&(GENERIC_READ|GENERIC_WRITE))
909 case GENERIC_READ:
910 mode |= OF_READ;
911 break;
912 case GENERIC_WRITE:
913 mode |= OF_WRITE;
914 break;
915 case (GENERIC_READ|GENERIC_WRITE):
916 mode |= OF_READWRITE;
917 break;
920 switch(sharing&(FILE_SHARE_READ|FILE_SHARE_WRITE))
922 case (FILE_SHARE_READ|FILE_SHARE_WRITE):
923 mode |= OF_SHARE_DENY_NONE;
924 break;
925 case FILE_SHARE_READ:
926 mode |= OF_SHARE_DENY_WRITE;
927 break;
928 case FILE_SHARE_WRITE:
929 mode |= OF_SHARE_DENY_READ;
930 break;
931 default:
932 mode |= OF_SHARE_EXCLUSIVE;
933 break;
936 return mode;
939 #if 0 /* not yet */
940 /* inverse of FILE_ConvertOFMode */
941 static BOOL SMB_OpenAndX(int fd, USHORT tree_id, USHORT user_id, USHORT dialect,
942 LPCSTR filename, DWORD access, DWORD sharing,
943 DWORD creation, DWORD attributes, USHORT *file_id )
945 unsigned char buffer[0x100];
946 int len = 0;
947 USHORT mode;
949 TRACE("%s\n",filename);
951 mode = SMB_GetMode(access,sharing);
953 memset(buffer,0,sizeof(buffer));
955 len = SMB_Header(buffer, SMB_COM_OPEN_ANDX, tree_id, user_id);
957 /* 0 */
958 buffer[len++] = 15; /* parameters */
959 buffer[len++] = 0xff; /* AndXCommand: secondary request */
960 buffer[len++] = 0x00; /* AndXReserved */
961 SMB_ADDWORD(buffer+len,0); len+=2; /* AndXOffset */
962 SMB_ADDWORD(buffer+len,0); len+=2; /* Flags */
963 SMB_ADDWORD(buffer+len,mode); len+=2; /* desired access */
964 SMB_ADDWORD(buffer+len,0); len+=2; /* search attributes */
965 SMB_ADDWORD(buffer+len,0); len+=2;
967 /*FIXME: complete */
968 return FALSE;
970 #endif
973 static BOOL SMB_Open(int fd, USHORT tree_id, USHORT user_id, USHORT dialect,
974 LPCSTR filename, DWORD access, DWORD sharing,
975 DWORD creation, DWORD attributes, USHORT *file_id )
977 unsigned char buf[0x100];
978 int slen,pcount,i;
979 USHORT mode = SMB_GetMode(access,sharing);
980 struct NB_Buffer rx,tx;
982 TRACE("%s\n",filename);
984 memset(buf,0,sizeof(buf));
986 tx.buffer = buf;
987 tx.len = SMB_Header(tx.buffer, SMB_COM_OPEN, tree_id, user_id);
989 /* 0 */
990 tx.buffer[tx.len++] = 2; /* parameters */
991 SMB_ADDWORD(tx.buffer+tx.len,mode); tx.len+=2;
992 SMB_ADDWORD(tx.buffer+tx.len,0); tx.len+=2; /* search attributes */
994 slen = strlen(filename)+2; /* inc. nul and BufferFormat */
995 SMB_ADDWORD(tx.buffer+tx.len,slen); tx.len+=2;
997 tx.buffer[tx.len] = 0x04; /* BufferFormat */
998 strcpy(&tx.buffer[tx.len+1],filename);
999 tx.len += slen;
1001 rx.buffer = NULL;
1002 rx.len = 0;
1003 if(!NB_Transaction(fd, &tx, &rx))
1004 return FALSE;
1006 if(!rx.buffer)
1007 return FALSE;
1009 if(SMB_GetError(rx.buffer))
1010 return FALSE;
1012 pcount = SMB_PARAM_COUNT(rx.buffer);
1014 if( (SMB_HDRSIZE+pcount*2) > rx.len )
1016 ERR("Bad parameter count %d\n",pcount);
1017 return FALSE;
1020 TRACE("response, %d args: ",pcount);
1021 for(i=0; i<pcount; i++)
1022 TRACE("%04x ",SMB_PARAM(rx.buffer,i));
1023 TRACE("\n");
1025 *file_id = SMB_PARAM(rx.buffer,0);
1027 TRACE("file_id = %04x\n",*file_id);
1029 return TRUE;
1033 static BOOL SMB_Read(int fd, USHORT tree_id, USHORT user_id, USHORT dialect,
1034 USHORT file_id, DWORD offset, LPVOID out, USHORT count, USHORT* read)
1036 int buf_size,n,i;
1037 struct NB_Buffer rx,tx;
1039 TRACE("user %04x tree %04x file %04x count %04x offset %08lx\n",
1040 user_id, tree_id, file_id, count, offset);
1042 buf_size = count+0x100;
1043 tx.buffer = (unsigned char *) RtlAllocateHeap(ntdll_get_process_heap(),0,buf_size);
1045 memset(tx.buffer,0,buf_size);
1047 tx.len = SMB_Header(tx.buffer, SMB_COM_READ, tree_id, user_id);
1049 tx.buffer[tx.len++] = 5;
1050 SMB_ADDWORD(&tx.buffer[tx.len],file_id); tx.len += 2;
1051 SMB_ADDWORD(&tx.buffer[tx.len],count); tx.len += 2;
1052 SMB_ADDDWORD(&tx.buffer[tx.len],offset); tx.len += 4;
1053 SMB_ADDWORD(&tx.buffer[tx.len],0); tx.len += 2; /* how many more bytes will be read */
1055 tx.buffer[tx.len++] = 0;
1057 rx.buffer = NULL;
1058 rx.len = 0;
1059 if(!NB_Transaction(fd, &tx, &rx))
1061 RtlFreeHeap(ntdll_get_process_heap(),0,tx.buffer);
1062 return FALSE;
1065 if(SMB_GetError(rx.buffer))
1067 RtlFreeHeap(ntdll_get_process_heap(),0,rx.buffer);
1068 RtlFreeHeap(ntdll_get_process_heap(),0,tx.buffer);
1069 return FALSE;
1072 n = SMB_PARAM_COUNT(rx.buffer);
1074 if( (SMB_HDRSIZE+n*2) > rx.len )
1076 RtlFreeHeap(ntdll_get_process_heap(),0,rx.buffer);
1077 RtlFreeHeap(ntdll_get_process_heap(),0,tx.buffer);
1078 ERR("Bad parameter count %d\n",n);
1079 return FALSE;
1082 TRACE("response, %d args: ",n);
1083 for(i=0; i<n; i++)
1084 TRACE("%04x ",SMB_PARAM(rx.buffer,i));
1085 TRACE("\n");
1087 n = SMB_PARAM(rx.buffer,5) - 3;
1088 if(n>count)
1089 n=count;
1091 memcpy( out, &SMB_BUFFER(rx.buffer,3), n);
1093 TRACE("Read %d bytes\n",n);
1094 *read = n;
1096 RtlFreeHeap(ntdll_get_process_heap(),0,tx.buffer);
1097 RtlFreeHeap(ntdll_get_process_heap(),0,rx.buffer);
1099 return TRUE;
1104 * setup_count : number of USHORTs in the setup string
1106 struct SMB_Trans2Info
1108 struct NB_Buffer buf;
1109 unsigned char *setup;
1110 int setup_count;
1111 unsigned char *params;
1112 int param_count;
1113 unsigned char *data;
1114 int data_count;
1118 * Do an SMB transaction
1120 * This function allocates memory in the recv structure. It is
1121 * the caller's responsibility to free the memory if it finds
1122 * that recv->buf.buffer is nonzero.
1124 static BOOL SMB_Transaction2(int fd, int tree_id, int user_id,
1125 struct SMB_Trans2Info *send,
1126 struct SMB_Trans2Info *recv)
1128 int buf_size;
1129 const int retmaxparams = 0xf000;
1130 const int retmaxdata = 1024;
1131 const int retmaxsetup = 0; /* FIXME */
1132 const int flags = 0;
1133 const int timeout = 0;
1134 int param_ofs, data_ofs;
1135 struct NB_Buffer tx;
1136 BOOL ret = FALSE;
1138 buf_size = 0x100 + send->setup_count*2 + send->param_count + send->data_count ;
1139 tx.buffer = (unsigned char *) RtlAllocateHeap(ntdll_get_process_heap(),0,buf_size);
1141 tx.len = SMB_Header(tx.buffer, SMB_COM_TRANSACTION2, tree_id, user_id);
1143 tx.buffer[tx.len++] = 14 + send->setup_count;
1144 SMB_ADDWORD(&tx.buffer[tx.len],send->param_count); /* total param bytes sent */
1145 tx.len += 2;
1146 SMB_ADDWORD(&tx.buffer[tx.len],send->data_count); /* total data bytes sent */
1147 tx.len += 2;
1148 SMB_ADDWORD(&tx.buffer[tx.len],retmaxparams); /*max parameter bytes to return */
1149 tx.len += 2;
1150 SMB_ADDWORD(&tx.buffer[tx.len],retmaxdata); /* max data bytes to return */
1151 tx.len += 2;
1152 tx.buffer[tx.len++] = retmaxsetup;
1153 tx.buffer[tx.len++] = 0; /* reserved1 */
1155 SMB_ADDWORD(&tx.buffer[tx.len],flags); /* flags */
1156 tx.len += 2;
1157 SMB_ADDDWORD(&tx.buffer[tx.len],timeout); /* timeout */
1158 tx.len += 4;
1159 SMB_ADDWORD(&tx.buffer[tx.len],0); /* reserved2 */
1160 tx.len += 2;
1161 SMB_ADDWORD(&tx.buffer[tx.len],send->param_count); /* parameter count - this buffer */
1162 tx.len += 2;
1164 param_ofs = tx.len; /* parameter offset */
1165 tx.len += 2;
1166 SMB_ADDWORD(&tx.buffer[tx.len],send->data_count); /* data count */
1167 tx.len += 2;
1169 data_ofs = tx.len; /* data offset */
1170 tx.len += 2;
1171 tx.buffer[tx.len++] = send->setup_count; /* setup count */
1172 tx.buffer[tx.len++] = 0; /* reserved3 */
1174 memcpy(&tx.buffer[tx.len], send->setup, send->setup_count*2); /* setup */
1175 tx.len += send->setup_count*2;
1177 /* add string here when implementing SMB_COM_TRANS */
1179 SMB_ADDWORD(&tx.buffer[param_ofs], tx.len);
1180 memcpy(&tx.buffer[tx.len], send->params, send->param_count); /* parameters */
1181 tx.len += send->param_count;
1182 if(tx.len%2)
1183 tx.len ++; /* pad2 */
1185 SMB_ADDWORD(&tx.buffer[data_ofs], tx.len);
1186 if(send->data_count && send->data)
1188 memcpy(&tx.buffer[tx.len], send->data, send->data_count); /* data */
1189 tx.len += send->data_count;
1192 recv->buf.buffer = NULL;
1193 recv->buf.len = 0;
1194 if(!NB_Transaction(fd, &tx, &recv->buf))
1195 goto done;
1197 if(!recv->buf.buffer)
1198 goto done;
1200 if(SMB_GetError(recv->buf.buffer))
1201 goto done;
1203 /* reuse these two offsets to check the received message */
1204 param_ofs = SMB_PARAM(recv->buf.buffer,4);
1205 data_ofs = SMB_PARAM(recv->buf.buffer,7);
1207 if( (recv->param_count + param_ofs) > recv->buf.len )
1208 goto done;
1210 if( (recv->data_count + data_ofs) > recv->buf.len )
1211 goto done;
1213 TRACE("Success\n");
1215 recv->setup = NULL;
1216 recv->setup_count = 0;
1218 recv->param_count = SMB_PARAM(recv->buf.buffer,0);
1219 recv->params = &recv->buf.buffer[param_ofs];
1221 recv->data_count = SMB_PARAM(recv->buf.buffer,6);
1222 recv->data = &recv->buf.buffer[data_ofs];
1225 TRACE("%d words\n",SMB_PARAM_COUNT(recv->buf.buffer));
1226 TRACE("total parameters = %d\n",SMB_PARAM(recv->buf.buffer,0));
1227 TRACE("total data = %d\n",SMB_PARAM(recv->buf.buffer,1));
1228 TRACE("parameters = %d\n",SMB_PARAM(recv->buf.buffer,3));
1229 TRACE("parameter offset = %d\n",SMB_PARAM(recv->buf.buffer,4));
1230 TRACE("param displace = %d\n",SMB_PARAM(recv->buf.buffer,5));
1232 TRACE("data count = %d\n",SMB_PARAM(recv->buf.buffer,6));
1233 TRACE("data offset = %d\n",SMB_PARAM(recv->buf.buffer,7));
1234 TRACE("data displace = %d\n",SMB_PARAM(recv->buf.buffer,8));
1237 ret = TRUE;
1239 done:
1240 if(tx.buffer)
1241 RtlFreeHeap(ntdll_get_process_heap(),0,tx.buffer);
1243 return ret;
1246 static BOOL SMB_SetupFindFirst(struct SMB_Trans2Info *send, LPSTR filename)
1248 int search_attribs = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
1249 int search_count = 10;
1250 int flags = 0;
1251 int infolevel = 0x104; /* SMB_FILE_BOTH_DIRECTORY_INFO */
1252 int storagetype = 0;
1253 int len, buf_size;
1255 memset(send,0,sizeof(send));
1257 send->setup_count = 1;
1258 send->setup = RtlAllocateHeap(ntdll_get_process_heap(),0,send->setup_count*2);
1259 if(!send->setup)
1260 return FALSE;
1262 buf_size = 0x10 + strlen(filename);
1263 send->params = RtlAllocateHeap(ntdll_get_process_heap(),0,buf_size);
1264 if(!send->params)
1266 RtlFreeHeap(ntdll_get_process_heap(),0,send->setup);
1267 return FALSE;
1270 SMB_ADDWORD(send->setup,TRANS2_FIND_FIRST2);
1272 len = 0;
1273 memset(send->params,0,buf_size);
1274 SMB_ADDWORD(&send->params[len],search_attribs); len += 2;
1275 SMB_ADDWORD(&send->params[len],search_count); len += 2;
1276 SMB_ADDWORD(&send->params[len],flags); len += 2;
1277 SMB_ADDWORD(&send->params[len],infolevel); len += 2;
1278 SMB_ADDDWORD(&send->params[len],storagetype); len += 4;
1280 strcpy(&send->params[len],filename);
1281 len += strlen(filename)+1;
1283 send->param_count = len;
1284 send->data = NULL;
1285 send->data_count = 0;
1287 return TRUE;
1290 static SMB_DIR *SMB_Trans2FindFirst(int fd, USHORT tree_id,
1291 USHORT user_id, USHORT dialect, LPSTR filename )
1293 int num;
1294 BOOL ret;
1295 /* char *filename = "\\*"; */
1296 struct SMB_Trans2Info send, recv;
1297 SMB_DIR *smbdir = NULL;
1299 TRACE("pattern = %s\n",filename);
1301 if(!SMB_SetupFindFirst(&send, filename))
1302 return FALSE;
1304 memset(&recv,0,sizeof(recv));
1306 ret = SMB_Transaction2(fd, tree_id, user_id, &send, &recv);
1307 RtlFreeHeap(ntdll_get_process_heap(),0,send.params);
1308 RtlFreeHeap(ntdll_get_process_heap(),0,send.setup);
1310 if(!ret)
1311 goto done;
1313 if(recv.setup_count)
1314 goto done;
1316 if(recv.param_count != 10)
1317 goto done;
1319 num = SMB_GETWORD(&recv.params[2]);
1320 TRACE("Success, search id: %d\n",num);
1322 if(SMB_GETWORD(&recv.params[4]))
1323 FIXME("need to read more!\n");
1325 smbdir = RtlAllocateHeap(ntdll_get_process_heap(),0,sizeof(*smbdir));
1326 if(smbdir)
1328 int i, ofs=0;
1330 smbdir->current = 0;
1331 smbdir->num_entries = num;
1332 smbdir->entries = RtlAllocateHeap(ntdll_get_process_heap(), 0, sizeof(unsigned char*)*num);
1333 if(!smbdir->entries)
1334 goto done;
1335 smbdir->buffer = recv.buf.buffer; /* save to free later */
1337 for(i=0; i<num; i++)
1339 int size = SMB_GETDWORD(&recv.data[ofs]);
1341 smbdir->entries[i] = &recv.data[ofs];
1343 if(TRACE_ON(file))
1345 int j;
1346 for(j=0; j<size; j++)
1347 DPRINTF("%02x%c",recv.data[ofs+j],((j+1)%16)?' ':'\n');
1349 TRACE("file %d : %s\n", i, &recv.data[ofs+0x5e]);
1350 ofs += size;
1351 if(ofs>recv.data_count)
1352 goto done;
1355 ret = TRUE;
1358 done:
1359 if(!ret)
1361 if( recv.buf.buffer )
1362 RtlFreeHeap(ntdll_get_process_heap(),0,recv.buf.buffer);
1363 if( smbdir )
1365 if( smbdir->entries )
1366 RtlFreeHeap(ntdll_get_process_heap(),0,smbdir->entries);
1367 RtlFreeHeap(ntdll_get_process_heap(),0,smbdir);
1369 smbdir = NULL;
1372 return smbdir;
1375 static int SMB_GetSocket(LPCSTR host)
1377 int fd=-1,r;
1378 struct sockaddr_in sin;
1379 struct hostent *he;
1381 TRACE("host %s\n",host);
1383 he = gethostbyname(host);
1384 if(he)
1386 memcpy(&sin.sin_addr,he->h_addr, sizeof (sin.sin_addr));
1387 goto connect;
1390 if(NB_Lookup(host,&sin))
1391 goto connect;
1393 /* FIXME: resolve by WINS too */
1395 ERR("couldn't resolve SMB host %s\n", host);
1397 return -1;
1399 connect:
1400 sin.sin_family = AF_INET;
1401 sin.sin_port = htons(139); /* netbios session */
1403 fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
1404 if(fd<0)
1405 return fd;
1408 unsigned char *x = (unsigned char *)&sin.sin_addr;
1409 TRACE("Connecting to %d.%d.%d.%d ...\n", x[0],x[1],x[2],x[3]);
1411 r = connect(fd, (struct sockaddr*)&sin, sizeof(sin));
1413 if(!NB_SessionReq(fd, "*SMBSERVER", "WINE"))
1415 close(fd);
1416 return -1;
1419 return fd;
1422 static BOOL SMB_LoginAndConnect(int fd, LPCSTR host, LPCSTR share, USHORT *tree_id, USHORT *user_id, USHORT *dialect)
1424 LPSTR name=NULL;
1426 TRACE("host %s share %s\n",host,share);
1428 if(!SMB_NegotiateProtocol(fd, dialect))
1429 return FALSE;
1431 if(!SMB_SessionSetup(fd, user_id))
1432 return FALSE;
1434 name = RtlAllocateHeap(ntdll_get_process_heap(),0,strlen(host)+strlen(share)+5);
1435 if(!name)
1436 return FALSE;
1438 sprintf(name,"\\\\%s\\%s",host,share);
1439 if(!SMB_TreeConnect(fd,*user_id,name,tree_id))
1441 RtlFreeHeap(ntdll_get_process_heap(),0,name);
1442 return FALSE;
1445 return TRUE;
1448 static HANDLE SMB_RegisterFile( int fd, USHORT tree_id, USHORT user_id, USHORT dialect, USHORT file_id)
1450 int r;
1451 HANDLE ret;
1453 wine_server_send_fd( fd );
1455 SERVER_START_REQ( create_smb )
1457 req->tree_id = tree_id;
1458 req->user_id = user_id;
1459 req->file_id = file_id;
1460 req->dialect = 0;
1461 req->fd = fd;
1462 SetLastError(0);
1463 r = wine_server_call_err( req );
1464 ret = reply->handle;
1466 SERVER_END_REQ;
1468 if(!r)
1469 TRACE("created wineserver smb object, handle = %p\n",ret);
1470 else
1471 SetLastError( ERROR_PATH_NOT_FOUND );
1473 return ret;
1476 HANDLE WINAPI SMB_CreateFileW( LPCWSTR uncname, DWORD access, DWORD sharing,
1477 LPSECURITY_ATTRIBUTES sa, DWORD creation,
1478 DWORD attributes, HANDLE template )
1480 int fd;
1481 USHORT tree_id=0, user_id=0, dialect=0, file_id=0;
1482 LPSTR name,host,share,file;
1483 HANDLE handle = INVALID_HANDLE_VALUE;
1484 INT len;
1486 len = WideCharToMultiByte(CP_ACP, 0, uncname, -1, NULL, 0, NULL, NULL);
1487 name = RtlAllocateHeap(ntdll_get_process_heap(), 0, len);
1488 if(!name)
1489 return handle;
1491 WideCharToMultiByte(CP_ACP, 0, uncname, -1, name, len, NULL, NULL);
1493 if( !UNC_SplitName(name, &host, &share, &file) )
1495 RtlFreeHeap(ntdll_get_process_heap(),0,name);
1496 return handle;
1499 TRACE("server is %s, share is %s, file is %s\n", host, share, file);
1501 fd = SMB_GetSocket(host);
1502 if(fd < 0)
1503 goto done;
1505 if(!SMB_LoginAndConnect(fd, host, share, &tree_id, &user_id, &dialect))
1506 goto done;
1508 #if 0
1509 if(!SMB_NtCreateOpen(fd, tree_id, user_id, dialect, file,
1510 access, sharing, sa, creation, attributes, template, &file_id ))
1512 close(fd);
1513 ERR("CreateOpen failed\n");
1514 goto done;
1516 #endif
1517 if(!SMB_Open(fd, tree_id, user_id, dialect, file,
1518 access, sharing, creation, attributes, &file_id ))
1520 close(fd);
1521 ERR("CreateOpen failed\n");
1522 goto done;
1525 handle = SMB_RegisterFile(fd, tree_id, user_id, dialect, file_id);
1526 if(!handle)
1528 ERR("register failed\n");
1529 close(fd);
1532 done:
1533 RtlFreeHeap(ntdll_get_process_heap(),0,name);
1534 return handle;
1537 static NTSTATUS SMB_GetSmbInfo(HANDLE hFile, USHORT *tree_id, USHORT *user_id, USHORT *dialect, USHORT *file_id, LPDWORD offset)
1539 NTSTATUS status;
1541 SERVER_START_REQ( get_smb_info )
1543 req->handle = hFile;
1544 req->flags = 0;
1545 status = wine_server_call( req );
1546 if(tree_id)
1547 *tree_id = reply->tree_id;
1548 if(user_id)
1549 *user_id = reply->user_id;
1550 if(file_id)
1551 *file_id = reply->file_id;
1552 if(dialect)
1553 *dialect = reply->dialect;
1554 if(offset)
1555 *offset = reply->offset;
1557 SERVER_END_REQ;
1559 return status;
1562 static NTSTATUS SMB_SetOffset(HANDLE hFile, DWORD offset)
1564 NTSTATUS status;
1566 TRACE("offset = %08lx\n",offset);
1568 SERVER_START_REQ( get_smb_info )
1570 req->handle = hFile;
1571 req->flags = SMBINFO_SET_OFFSET;
1572 req->offset = offset;
1573 status = wine_server_call( req );
1574 /* if(offset)
1575 *offset = reply->offset; */
1577 SERVER_END_REQ;
1579 return status;
1582 NTSTATUS WINAPI SMB_ReadFile(HANDLE hFile, LPVOID buffer, DWORD bytesToRead,
1583 PIO_STATUS_BLOCK io_status)
1585 int fd;
1586 DWORD count, offset;
1587 USHORT user_id, tree_id, dialect, file_id, read;
1589 TRACE("%p %p %ld %p\n", hFile, buffer, bytesToRead, io_status);
1591 io_status->Information = 0;
1593 io_status->u.Status = SMB_GetSmbInfo(hFile, &tree_id, &user_id, &dialect, &file_id, &offset);
1594 if (io_status->u.Status) return io_status->u.Status;
1596 fd = FILE_GetUnixHandle(hFile, GENERIC_READ);
1597 if (fd<0) return io_status->u.Status = STATUS_INVALID_HANDLE;
1599 while(1)
1601 count = bytesToRead - io_status->Information;
1602 if(count>0x400)
1603 count = 0x400;
1604 if(count==0)
1605 break;
1606 read = 0;
1607 if (!SMB_Read(fd, tree_id, user_id, dialect, file_id, offset, buffer, count, &read))
1608 break;
1609 if(!read)
1610 break;
1611 io_status->Information += read;
1612 buffer = (char*)buffer + read;
1613 offset += read;
1614 if(io_status->Information >= bytesToRead)
1615 break;
1617 close(fd);
1619 return io_status->u.Status = SMB_SetOffset(hFile, offset);
1622 SMB_DIR* WINAPI SMB_FindFirst(LPCWSTR name)
1624 int fd = -1;
1625 LPSTR host,share,file;
1626 USHORT tree_id=0, user_id=0, dialect=0;
1627 SMB_DIR *ret = NULL;
1628 LPSTR filename;
1629 DWORD len;
1631 TRACE("Find %s\n",debugstr_w(name));
1633 len = WideCharToMultiByte( CP_ACP, 0, name, -1, NULL, 0, NULL, NULL );
1634 filename = RtlAllocateHeap(ntdll_get_process_heap(),0,len);
1635 if(!filename)
1636 return ret;
1637 WideCharToMultiByte( CP_ACP, 0, name, -1, filename, len, NULL, NULL );
1639 if( !UNC_SplitName(filename, &host, &share, &file) )
1640 goto done;
1642 fd = SMB_GetSocket(host);
1643 if(fd < 0)
1644 goto done;
1646 if(!SMB_LoginAndConnect(fd, host, share, &tree_id, &user_id, &dialect))
1647 goto done;
1649 TRACE("server is %s, share is %s, file is %s\n", host, share, file);
1651 ret = SMB_Trans2FindFirst(fd, tree_id, user_id, dialect, file);
1653 done:
1654 /* disconnect */
1655 if(fd != -1)
1656 close(fd);
1658 if(filename)
1659 RtlFreeHeap(ntdll_get_process_heap(),0,filename);
1661 return ret;
1665 BOOL WINAPI SMB_FindNext(SMB_DIR *dir, WIN32_FIND_DATAW *data )
1667 unsigned char *ent;
1668 int len, fnlen;
1670 TRACE("%d of %d\n",dir->current,dir->num_entries);
1672 if(dir->current >= dir->num_entries)
1673 return FALSE;
1675 memset(data, 0, sizeof(*data));
1677 ent = dir->entries[dir->current];
1678 len = SMB_GETDWORD(&ent[0]);
1679 if(len<0x5e)
1680 return FALSE;
1682 memcpy(&data->ftCreationTime, &ent[8], 8);
1683 memcpy(&data->ftLastAccessTime, &ent[0x10], 8);
1684 memcpy(&data->ftLastWriteTime, &ent[0x18], 8);
1685 data->nFileSizeHigh = SMB_GETDWORD(&ent[0x30]);
1686 data->nFileSizeLow = SMB_GETDWORD(&ent[0x34]);
1687 data->dwFileAttributes = SMB_GETDWORD(&ent[0x38]);
1689 /* copy the long filename */
1690 fnlen = SMB_GETDWORD(&ent[0x3c]);
1691 if ( fnlen > (sizeof(data->cFileName)/sizeof(WCHAR)) )
1692 return FALSE;
1693 MultiByteToWideChar( CP_ACP, 0, &ent[0x5e], fnlen, data->cFileName,
1694 sizeof(data->cFileName)/sizeof(WCHAR) );
1696 /* copy the short filename */
1697 if ( ent[0x44] > (sizeof(data->cAlternateFileName)/sizeof(WCHAR)) )
1698 return FALSE;
1699 MultiByteToWideChar( CP_ACP, 0, &ent[0x5e + len], ent[0x44], data->cAlternateFileName,
1700 sizeof(data->cAlternateFileName)/sizeof(WCHAR) );
1702 dir->current++;
1704 return TRUE;
1707 BOOL WINAPI SMB_CloseDir(SMB_DIR *dir)
1709 RtlFreeHeap(ntdll_get_process_heap(),0,dir->buffer);
1710 RtlFreeHeap(ntdll_get_process_heap(),0,dir->entries);
1711 memset(dir,0,sizeof(*dir));
1712 RtlFreeHeap(ntdll_get_process_heap(),0,dir);
1713 return TRUE;