Rewrote the collapsing of . and .. in RtlGetFullPathName_U for better
[wine.git] / files / smb.c
blob9ab9f9215adf65321ba52441814253f9651e6fe5
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 <stdarg.h>
64 #include <stdio.h>
65 #include <string.h>
66 #include <sys/types.h>
67 #include <sys/stat.h>
68 #ifdef HAVE_SYS_MMAN_H
69 #include <sys/mman.h>
70 #endif
71 #ifdef HAVE_SYS_TIME_H
72 # include <sys/time.h>
73 #endif
74 #ifdef HAVE_SYS_POLL_H
75 # include <sys/poll.h>
76 #endif
77 #include <time.h>
78 #ifdef HAVE_UNISTD_H
79 # include <unistd.h>
80 #endif
81 #ifdef HAVE_UTIME_H
82 # include <utime.h>
83 #endif
84 #ifdef HAVE_SYS_SOCKET_H
85 # include <sys/socket.h>
86 #endif
87 #include <sys/types.h>
88 #ifdef HAVE_NETINET_IN_SYSTM_H
89 #include <netinet/in_systm.h>
90 #endif
91 #ifdef HAVE_NETINET_IN_H
92 #include <netinet/in.h>
93 #endif
94 #ifdef HAVE_NETINET_IP_H
95 #include <netinet/ip.h>
96 #endif
97 #ifdef HAVE_ARPA_INET_H
98 #include <arpa/inet.h>
99 #endif
100 #ifdef HAVE_NETDB_H
101 #include <netdb.h>
102 #endif
104 #define NONAMELESSUNION
105 #define NONAMELESSSTRUCT
106 #include "winerror.h"
107 #include "ntstatus.h"
108 #include "windef.h"
109 #include "winbase.h"
110 #include "winnls.h"
111 #include "winreg.h"
112 #include "winternl.h"
113 #include "file.h"
114 #include "smb.h"
116 #include "wine/server.h"
117 #include "wine/debug.h"
119 WINE_DEFAULT_DEBUG_CHANNEL(file);
121 #define NBR_ADDWORD(p,word) { (p)[1] = (word & 0xff); (p)[0] = ((word)>>8)&0xff; }
122 #define NBR_GETWORD(p) ( (((p)[0])<<8) | ((p)[1]) )
124 #define SMB_ADDWORD(p,word) { (p)[0] = (word & 0xff); (p)[1] = ((word)>>8)&0xff; }
125 #define SMB_GETWORD(p) ( (((p)[1])<<8) | ((p)[0]) )
126 #define SMB_ADDDWORD(p,w) { (p)[3]=((w)>>24)&0xff; (p)[2]=((w)>>16)&0xff; (p)[1]=((w)>>8)&0xff; (p)[0]=(w)&0xff; }
127 #define SMB_GETDWORD(p) ( (((p)[3])<<24) | (((p)[2])<<16) | (((p)[1])<<8) | ((p)[0]) )
129 #define SMB_COM_CREATE_DIRECTORY 0x00
130 #define SMB_COM_DELETE_DIRECTORY 0x01
131 #define SMB_COM_OPEN 0x02
132 #define SMB_COM_CREATE 0x03
133 #define SMB_COM_CLOSE 0x04
134 #define SMB_COM_FLUSH 0x05
135 #define SMB_COM_DELETE 0x06
136 #define SMB_COM_RENAME 0x07
137 #define SMB_COM_QUERY_INFORMATION 0x08
138 #define SMB_COM_SET_INFORMATION 0x09
139 #define SMB_COM_READ 0x0A
140 #define SMB_COM_WRITE 0x0B
141 #define SMB_COM_LOCK_BYTE_RANGE 0x0C
142 #define SMB_COM_UNLOCK_BYTE_RANGE 0x0D
143 #define SMB_COM_CREATE_TEMPORARY 0x0E
144 #define SMB_COM_CREATE_NEW 0x0F
145 #define SMB_COM_CHECK_DIRECTORY 0x10
146 #define SMB_COM_PROCESS_EXIT 0x11
147 #define SMB_COM_SEEK 0x12
148 #define SMB_COM_LOCK_AND_READ 0x13
149 #define SMB_COM_WRITE_AND_UNLOCK 0x14
150 #define SMB_COM_READ_RAW 0x1A
151 #define SMB_COM_READ_MPX 0x1B
152 #define SMB_COM_READ_MPX_SECONDARY 0x1C
153 #define SMB_COM_WRITE_RAW 0x1D
154 #define SMB_COM_WRITE_MPX 0x1E
155 #define SMB_COM_WRITE_COMPLETE 0x20
156 #define SMB_COM_SET_INFORMATION2 0x22
157 #define SMB_COM_QUERY_INFORMATION2 0x23
158 #define SMB_COM_LOCKING_ANDX 0x24
159 #define SMB_COM_TRANSACTION 0x25
160 #define SMB_COM_TRANSACTION_SECONDARY 0x26
161 #define SMB_COM_IOCTL 0x27
162 #define SMB_COM_IOCTL_SECONDARY 0x28
163 #define SMB_COM_COPY 0x29
164 #define SMB_COM_MOVE 0x2A
165 #define SMB_COM_ECHO 0x2B
166 #define SMB_COM_WRITE_AND_CLOSE 0x2C
167 #define SMB_COM_OPEN_ANDX 0x2D
168 #define SMB_COM_READ_ANDX 0x2E
169 #define SMB_COM_WRITE_ANDX 0x2F
170 #define SMB_COM_CLOSE_AND_TREE_DISC 0x31
171 #define SMB_COM_TRANSACTION2 0x32
172 #define SMB_COM_TRANSACTION2_SECONDARY 0x33
173 #define SMB_COM_FIND_CLOSE2 0x34
174 #define SMB_COM_FIND_NOTIFY_CLOSE 0x35
175 #define SMB_COM_TREE_CONNECT 0x70
176 #define SMB_COM_TREE_DISCONNECT 0x71
177 #define SMB_COM_NEGOTIATE 0x72
178 #define SMB_COM_SESSION_SETUP_ANDX 0x73
179 #define SMB_COM_LOGOFF_ANDX 0x74
180 #define SMB_COM_TREE_CONNECT_ANDX 0x75
181 #define SMB_COM_QUERY_INFORMATION_DISK 0x80
182 #define SMB_COM_SEARCH 0x81
183 #define SMB_COM_FIND 0x82
184 #define SMB_COM_FIND_UNIQUE 0x83
185 #define SMB_COM_NT_TRANSACT 0xA0
186 #define SMB_COM_NT_TRANSACT_SECONDARY 0xA1
187 #define SMB_COM_NT_CREATE_ANDX 0xA2
188 #define SMB_COM_NT_CANCEL 0xA4
189 #define SMB_COM_OPEN_PRINT_FILE 0xC0
190 #define SMB_COM_WRITE_PRINT_FILE 0xC1
191 #define SMB_COM_CLOSE_PRINT_FILE 0xC2
192 #define SMB_COM_GET_PRINT_QUEUE 0xC3
194 #define TRANS2_FIND_FIRST2 0x01
195 #define TRANS2_FIND_NEXT2 0x02
197 #define MAX_HOST_NAME 15
198 #define NB_TIMEOUT 10000
200 /* We only need the A versions locally currently */
201 static inline int SMB_isSepA (CHAR c) {return (c == '\\' || c == '/');}
202 static inline int SMB_isUNCA (LPCSTR filename) {return (filename && SMB_isSepW (filename[0]) && SMB_isSepW (filename[1]));}
203 static inline CHAR *SMB_nextSepA (CHAR *s) {while (*s && !SMB_isSepA (*s)) s++; return (*s? s : 0);}
204 /* NB SM_nextSepA cannot return const CHAR * since it is going to be used for
205 * replacing separators with null characters
208 static USHORT SMB_MultiplexId = 0;
210 struct NB_Buffer
212 unsigned char *buffer;
213 int len;
216 static int netbios_name(const char *p, unsigned char *buffer)
218 char ch;
219 int i,len=0;
221 buffer[len++]=' ';
222 for(i=0; i<=MAX_HOST_NAME; i++)
224 if(i<MAX_HOST_NAME)
226 if(*p)
227 ch = *p++&0xdf; /* add character from hostname */
228 else
229 ch = ' '; /* add padding */
231 else
232 ch = 0; /* add terminator */
233 buffer[len++] = ((ch&0xf0) >> 4) + 'A';
234 buffer[len++] = (ch&0x0f) + 'A';
236 buffer[len++] = 0; /* add second terminator */
237 return len;
240 static DWORD NB_NameReq(LPCSTR host, unsigned char *buffer, int len)
242 int trn = 1234,i=0;
244 NBR_ADDWORD(&buffer[i],trn); i+=2;
245 NBR_ADDWORD(&buffer[i],0x0110); i+=2;
246 NBR_ADDWORD(&buffer[i],0x0001); i+=2;
247 NBR_ADDWORD(&buffer[i],0x0000); i+=2;
248 NBR_ADDWORD(&buffer[i],0x0000); i+=2;
249 NBR_ADDWORD(&buffer[i],0x0000); i+=2;
251 i += netbios_name(host,&buffer[i]);
253 NBR_ADDWORD(&buffer[i],0x0020); i+=2;
254 NBR_ADDWORD(&buffer[i],0x0001); i+=2;
256 TRACE("packet is %d bytes in length\n",i);
259 int j;
260 for(j=0; j<i; j++)
261 printf("%02x%c",buffer[j],(((j+1)%16)&&((j+1)!=j))?' ':'\n');
264 return i;
267 /* unc = \\hostname\share\file... */
268 static BOOL UNC_SplitName(LPSTR unc, LPSTR *hostname, LPSTR *share, LPSTR *file)
270 char *p;
272 TRACE("%s\n",unc);
274 if (!SMB_isUNCA (unc))
275 return FALSE;
276 p = unc + 2;
277 *hostname=p;
279 p = SMB_nextSepA (p);
280 if(!p)
281 return FALSE;
282 *p=0;
283 *share = ++p;
285 p = SMB_nextSepA (p);
286 if(!p)
287 return FALSE;
288 *p=0;
289 *file = ++p;
291 return TRUE;
294 static BOOL NB_Lookup(LPCSTR host, struct sockaddr_in *addr)
296 int fd,on=1,r,len,i,fromsize;
297 struct pollfd fds;
298 struct sockaddr_in sin,fromaddr;
299 unsigned char buffer[256];
301 fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
302 if(fd<0)
303 return FALSE;
305 r = setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));
306 if(r<0)
307 goto err;
309 sin.sin_family = AF_INET;
310 sin.sin_port = htons(137);
311 sin.sin_addr.s_addr = 0xffffffff;
313 len = NB_NameReq(host,buffer,sizeof(buffer));
314 if(len<=0)
315 goto err;
317 r = sendto(fd, buffer, len, 0, (struct sockaddr*)&sin, sizeof(sin));
318 if(r<0)
320 FIXME("Error sending packet\n");
321 goto err;
324 fds.fd = fd;
325 fds.events = POLLIN;
326 fds.revents = 0;
328 /* FIXME: this is simple and easily fooled logic
329 * we should loop until we receive the correct packet or timeout
331 r = poll(&fds,1,NB_TIMEOUT);
332 if(r!=1)
333 goto err;
335 TRACE("Got response!\n");
337 fromsize = sizeof (fromaddr);
338 r = recvfrom(fd, buffer, sizeof(buffer), 0, (struct sockaddr*)&fromaddr, &fromsize);
339 if(r<0)
340 goto err;
342 TRACE("%d bytes received\n",r);
344 if(r!=62)
345 goto err;
347 for(i=0; i<r; i++)
348 DPRINTF("%02X%c",buffer[i],(((i+1)!=r)&&((i+1)%16))?' ':'\n');
349 DPRINTF("\n");
351 if(0x0f & buffer[3])
352 goto err;
354 TRACE("packet is OK\n");
356 memcpy(&addr->sin_addr, &buffer[58], sizeof(addr->sin_addr));
358 close(fd);
359 return TRUE;
361 err:
362 close(fd);
363 return FALSE;
366 #define NB_FIRST 0x40
368 #define NB_HDRSIZE 4
370 #define NB_SESSION_MSG 0x00
371 #define NB_SESSION_REQ 0x81
373 /* RFC 1002, section 4.3.2 */
374 static BOOL NB_SessionReq(int fd, const char *called, const char *calling)
376 unsigned char buffer[0x100];
377 int len = 0,r;
378 struct pollfd fds;
380 TRACE("called %s, calling %s\n",called,calling);
382 buffer[0] = NB_SESSION_REQ;
383 buffer[1] = NB_FIRST;
385 netbios_name(called, &buffer[NB_HDRSIZE]);
386 len += 34;
387 netbios_name(calling, &buffer[NB_HDRSIZE+len]);
388 len += 34;
390 NBR_ADDWORD(&buffer[2],len);
392 /* for(i=0; i<(len+NB_HDRSIZE); i++)
393 DPRINTF("%02X%c",buffer[i],(((i+1)!=(len+4))&&((i+1)%16))?' ':'\n'); */
395 r = write(fd,buffer,len+4);
396 if(r<0)
398 ERR("Write failed\n");
399 return FALSE;
402 fds.fd = fd;
403 fds.events = POLLIN;
404 fds.revents = 0;
406 r = poll(&fds,1,NB_TIMEOUT);
407 if(r!=1)
409 ERR("Poll failed\n");
410 return FALSE;
413 r = read(fd, buffer, NB_HDRSIZE);
414 if((r!=NB_HDRSIZE) || (buffer[0]!=0x82))
416 TRACE("Received %d bytes\n",r);
417 TRACE("%02x %02x %02x %02x\n", buffer[0],buffer[1],buffer[2],buffer[3]);
418 return FALSE;
421 return TRUE;
424 static BOOL NB_SendData(int fd, struct NB_Buffer *out)
426 unsigned char buffer[NB_HDRSIZE];
427 int r;
429 /* CHECK: is it always OK to do this in two writes? */
430 /* perhaps use scatter gather sendmsg instead? */
432 buffer[0] = NB_SESSION_MSG;
433 buffer[1] = NB_FIRST;
434 NBR_ADDWORD(&buffer[2],out->len);
436 r = write(fd, buffer, NB_HDRSIZE);
437 if(r!=NB_HDRSIZE)
438 return FALSE;
440 r = write(fd, out->buffer, out->len);
441 if(r!=out->len)
443 ERR("write failed\n");
444 return FALSE;
447 return TRUE;
450 static BOOL NB_RecvData(int fd, struct NB_Buffer *rx)
452 int r;
453 unsigned char buffer[NB_HDRSIZE];
455 r = read(fd, buffer, NB_HDRSIZE);
456 if((r!=NB_HDRSIZE) || (buffer[0]!=NB_SESSION_MSG))
458 ERR("Received %d bytes\n",r);
459 return FALSE;
462 rx->len = NBR_GETWORD(&buffer[2]);
464 rx->buffer = RtlAllocateHeap(GetProcessHeap(), 0, rx->len);
465 if(!rx->buffer)
466 return FALSE;
468 r = read(fd, rx->buffer, rx->len);
469 if(rx->len!=r)
471 TRACE("Received %d bytes\n",r);
472 RtlFreeHeap(GetProcessHeap(), 0, rx->buffer);
473 rx->buffer = 0;
474 rx->len = 0;
475 return FALSE;
478 return TRUE;
481 static BOOL NB_Transaction(int fd, struct NB_Buffer *in, struct NB_Buffer *out)
483 int r;
484 struct pollfd fds;
486 if(TRACE_ON(file))
488 int i;
489 DPRINTF("Sending request:\n");
490 for(i=0; i<in->len; i++)
491 DPRINTF("%02X%c",in->buffer[i],(((i+1)!=in->len)&&((i+1)%16))?' ':'\n');
494 if(!NB_SendData(fd,in))
495 return FALSE;
497 fds.fd = fd;
498 fds.events = POLLIN;
499 fds.revents = 0;
501 r = poll(&fds,1,NB_TIMEOUT);
502 if(r!=1)
504 ERR("Poll failed\n");
505 return FALSE;
508 if(!NB_RecvData(fd, out))
509 return FALSE;
511 if(TRACE_ON(file))
513 int i;
514 DPRINTF("Got response:\n");
515 for(i=0; i<out->len; i++)
516 DPRINTF("%02X%c",out->buffer[i],(((i+1)!=out->len)&&((i+1)%16))?' ':'\n');
519 return TRUE;
522 #define SMB_ADDHEADER(b,l) { b[(l)++]=0xff; b[(l)++]='S'; b[(l)++]='M'; b[(l)++]='B'; }
523 #define SMB_ADDERRINFO(b,l) { b[(l)++]=0; b[(l)++]=0; b[(l)++]=0; b[(l)++]=0; }
524 #define SMB_ADDPADSIG(b,l) { memset(&b[l],0,12); l+=12; }
526 #define SMB_ERRCLASS 5
527 #define SMB_ERRCODE 7
528 #define SMB_TREEID 24
529 #define SMB_PROCID 26
530 #define SMB_USERID 28
531 #define SMB_PLEXID 30
532 #define SMB_PCOUNT 32
533 #define SMB_HDRSIZE 33
535 static DWORD SMB_GetError(unsigned char *buffer)
537 const char *err_class;
539 switch(buffer[SMB_ERRCLASS])
541 case 0:
542 return STATUS_SUCCESS;
543 case 1:
544 err_class = "DOS";
545 break;
546 case 2:
547 err_class = "net server";
548 break;
549 case 3:
550 err_class = "hardware";
551 break;
552 case 0xff:
553 err_class = "smb";
554 break;
555 default:
556 err_class = "unknown";
557 break;
560 ERR("%s error %d \n",err_class, buffer[SMB_ERRCODE]);
562 /* FIXME: return propper error codes */
563 return STATUS_INVALID_PARAMETER;
566 static int SMB_Header(unsigned char *buffer, unsigned char command, USHORT tree_id, USHORT user_id)
568 int len = 0;
569 DWORD id;
571 /* 0 */
572 SMB_ADDHEADER(buffer,len);
574 /* 4 */
575 buffer[len++] = command;
577 /* 5 */
578 SMB_ADDERRINFO(buffer,len)
580 /* 9 */
581 buffer[len++] = 0x00; /* flags */
582 SMB_ADDWORD(&buffer[len],1); len += 2; /* flags2 */
584 /* 12 */
585 SMB_ADDPADSIG(buffer,len)
587 /* 24 */
588 SMB_ADDWORD(&buffer[len],tree_id); len += 2; /* treeid */
589 id = GetCurrentThreadId();
590 SMB_ADDWORD(&buffer[len],id); len += 2; /* process id */
591 SMB_ADDWORD(&buffer[len],user_id); len += 2; /* user id */
592 SMB_ADDWORD(&buffer[len],SMB_MultiplexId); len += 2; /* multiplex id */
593 SMB_MultiplexId++;
595 return len;
598 static const char *SMB_ProtocolDialect = "NT LM 0.12";
599 /* = "Windows for Workgroups 3.1a"; */
601 /* FIXME: support multiple SMB dialects */
602 static BOOL SMB_NegotiateProtocol(int fd, USHORT *dialect)
604 unsigned char buf[0x100];
605 int buflen = 0;
606 struct NB_Buffer tx, rx;
608 TRACE("\n");
610 memset(buf,0,sizeof(buf));
612 tx.buffer = buf;
613 tx.len = SMB_Header(tx.buffer, SMB_COM_NEGOTIATE, 0, 0);
615 /* parameters */
616 tx.buffer[tx.len++] = 0; /* no parameters */
618 /* command buffer */
619 buflen = strlen(SMB_ProtocolDialect)+2; /* include type and nul byte */
620 SMB_ADDWORD(&tx.buffer[tx.len],buflen); tx.len += 2;
622 tx.buffer[tx.len] = 0x02;
623 strcpy(&tx.buffer[tx.len+1],SMB_ProtocolDialect);
624 tx.len += buflen;
626 rx.buffer = NULL;
627 rx.len = 0;
628 if(!NB_Transaction(fd, &tx, &rx))
630 ERR("Failed\n");
631 return FALSE;
634 if(!rx.buffer)
635 return FALSE;
637 /* FIXME: check response */
638 if(SMB_GetError(rx.buffer))
640 ERR("returned error\n");
641 RtlFreeHeap(GetProcessHeap(),0,rx.buffer);
642 return FALSE;
645 RtlFreeHeap(GetProcessHeap(),0,rx.buffer);
647 *dialect = 0;
649 return TRUE;
652 #define SMB_PARAM_COUNT(buffer) ((buffer)[SMB_PCOUNT])
653 #define SMB_PARAM(buffer,n) SMB_GETWORD(&(buffer)[SMB_HDRSIZE+2*(n)])
654 #define SMB_BUFFER_COUNT(buffer) SMB_GETWORD(buffer+SMB_HDRSIZE+2*SMB_PARAM_COUNT(buffer))
655 #define SMB_BUFFER(buffer,n) ((buffer)[SMB_HDRSIZE + 2*SMB_PARAM_COUNT(buffer) + 2 + (n) ])
657 static BOOL SMB_SessionSetup(int fd, USHORT *userid)
659 unsigned char buf[0x100];
660 int pcount,bcount;
661 struct NB_Buffer rx, tx;
663 memset(buf,0,sizeof(buf));
664 tx.buffer = buf;
666 tx.len = SMB_Header(tx.buffer, SMB_COM_SESSION_SETUP_ANDX, 0, 0);
668 tx.buffer[tx.len++] = 0; /* no parameters? */
670 tx.buffer[tx.len++] = 0xff; /* AndXCommand: secondary request */
671 tx.buffer[tx.len++] = 0x00; /* AndXReserved */
672 SMB_ADDWORD(&tx.buffer[tx.len],0); /* AndXOffset */
673 tx.len += 2;
674 SMB_ADDWORD(&tx.buffer[tx.len],0x400); /* MaxBufferSize */
675 tx.len += 2;
676 SMB_ADDWORD(&tx.buffer[tx.len],1); /* MaxMpxCount */
677 tx.len += 2;
678 SMB_ADDWORD(&tx.buffer[tx.len],0); /* VcNumber */
679 tx.len += 2;
680 SMB_ADDWORD(&tx.buffer[tx.len],0); /* SessionKey */
681 tx.len += 2;
682 SMB_ADDWORD(&tx.buffer[tx.len],0); /* SessionKey */
683 tx.len += 2;
684 SMB_ADDWORD(&tx.buffer[tx.len],0); /* Password length */
685 tx.len += 2;
686 SMB_ADDWORD(&tx.buffer[tx.len],0); /* Reserved */
687 tx.len += 2;
688 SMB_ADDWORD(&tx.buffer[tx.len],0); /* Reserved */
689 tx.len += 2;
691 /* FIXME: add name and password here */
692 tx.buffer[tx.len++] = 0; /* number of bytes in password */
694 rx.buffer = NULL;
695 rx.len = 0;
696 if(!NB_Transaction(fd, &tx, &rx))
697 return FALSE;
699 if(!rx.buffer)
700 return FALSE;
702 if(SMB_GetError(rx.buffer))
703 goto done;
705 pcount = SMB_PARAM_COUNT(rx.buffer);
707 if( (SMB_HDRSIZE+pcount*2) > rx.len )
709 ERR("Bad parameter count %d\n",pcount);
710 goto done;
713 if(TRACE_ON(file))
715 int i;
716 DPRINTF("SMB_COM_SESSION_SETUP response, %d args: ",pcount);
717 for(i=0; i<pcount; i++)
718 DPRINTF("%04x ",SMB_PARAM(rx.buffer,i));
719 DPRINTF("\n");
722 bcount = SMB_BUFFER_COUNT(rx.buffer);
723 if( (SMB_HDRSIZE+pcount*2+2+bcount) > rx.len )
725 ERR("parameter count %x, buffer count %x, len %x\n",pcount,bcount,rx.len);
726 goto done;
729 if(TRACE_ON(file))
731 int i;
732 DPRINTF("response buffer %d bytes: ",bcount);
733 for(i=0; i<bcount; i++)
735 unsigned char ch = SMB_BUFFER(rx.buffer,i);
736 DPRINTF("%c", isprint(ch)?ch:' ');
738 DPRINTF("\n");
741 *userid = SMB_GETWORD(&rx.buffer[SMB_USERID]);
743 RtlFreeHeap(GetProcessHeap(),0,rx.buffer);
744 return TRUE;
746 done:
747 RtlFreeHeap(GetProcessHeap(),0,rx.buffer);
748 return FALSE;
752 static BOOL SMB_TreeConnect(int fd, USHORT user_id, LPCSTR share_name, USHORT *treeid)
754 unsigned char buf[0x100];
755 int slen;
756 struct NB_Buffer rx,tx;
758 TRACE("%s\n",share_name);
760 memset(buf,0,sizeof(buf));
761 tx.buffer = buf;
763 tx.len = SMB_Header(tx.buffer, SMB_COM_TREE_CONNECT, 0, user_id);
765 tx.buffer[tx.len++] = 4; /* parameters */
767 tx.buffer[tx.len++] = 0xff; /* AndXCommand: secondary request */
768 tx.buffer[tx.len++] = 0x00; /* AndXReserved */
769 SMB_ADDWORD(&tx.buffer[tx.len],0); /* AndXOffset */
770 tx.len += 2;
771 SMB_ADDWORD(&tx.buffer[tx.len],0); /* Flags */
772 tx.len += 2;
773 SMB_ADDWORD(&tx.buffer[tx.len],1); /* Password length */
774 tx.len += 2;
776 /* SMB command buffer */
777 SMB_ADDWORD(&tx.buffer[tx.len],3); /* command buffer len */
778 tx.len += 2;
779 tx.buffer[tx.len++] = 0; /* null terminated password */
781 slen = strlen(share_name);
782 if(slen<(sizeof(buf)-tx.len))
783 strcpy(&tx.buffer[tx.len], share_name);
784 else
785 return FALSE;
786 tx.len += slen+1;
788 /* name of the service */
789 tx.buffer[tx.len++] = 0;
791 rx.buffer = NULL;
792 rx.len = 0;
793 if(!NB_Transaction(fd, &tx, &rx))
794 return FALSE;
796 if(!rx.buffer)
797 return FALSE;
799 if(SMB_GetError(rx.buffer))
801 RtlFreeHeap(GetProcessHeap(),0,rx.buffer);
802 return FALSE;
805 *treeid = SMB_GETWORD(&rx.buffer[SMB_TREEID]);
807 RtlFreeHeap(GetProcessHeap(),0,rx.buffer);
808 TRACE("OK, treeid = %04x\n", *treeid);
810 return TRUE;
813 #if 0 /* not yet */
814 static BOOL SMB_NtCreateOpen(int fd, USHORT tree_id, USHORT user_id, USHORT dialect,
815 LPCSTR filename, DWORD access, DWORD sharing,
816 LPSECURITY_ATTRIBUTES sa, DWORD creation,
817 DWORD attributes, HANDLE template, USHORT *file_id )
819 unsigned char buffer[0x100];
820 int len = 0,slen;
822 TRACE("%s\n",filename);
824 memset(buffer,0,sizeof(buffer));
826 len = SMB_Header(buffer, SMB_COM_NT_CREATE_ANDX, tree_id, user_id);
828 /* 0 */
829 buffer[len++] = 24; /* parameters */
831 buffer[len++] = 0xff; /* AndXCommand: secondary request */
832 buffer[len++] = 0x00; /* AndXReserved */
833 SMB_ADDWORD(&buffer[len],0); len += 2; /* AndXOffset */
835 buffer[len++] = 0; /* reserved */
836 slen = strlen(filename);
837 SMB_ADDWORD(&buffer[len],slen); len += 2; /* name length */
839 /* 0x08 */
840 SMB_ADDDWORD(&buffer[len],0); len += 4; /* flags */
841 SMB_ADDDWORD(&buffer[len],0); len += 4; /* root directory fid */
842 /* 0x10 */
843 SMB_ADDDWORD(&buffer[len],access); len += 4; /* access */
844 SMB_ADDDWORD(&buffer[len],0); len += 4; /* allocation size */
845 /* 0x18 */
846 SMB_ADDDWORD(&buffer[len],0); len += 4; /* root directory fid */
848 /* 0x1c */
849 SMB_ADDDWORD(&buffer[len],0); len += 4; /* initial allocation */
850 SMB_ADDDWORD(&buffer[len],0); len += 4;
852 /* 0x24 */
853 SMB_ADDDWORD(&buffer[len],attributes); len += 4; /* ExtFileAttributes*/
855 /* 0x28 */
856 SMB_ADDDWORD(&buffer[len],sharing); len += 4; /* ShareAccess */
858 /* 0x2c */
859 TRACE("creation = %08lx\n",creation);
860 SMB_ADDDWORD(&buffer[len],creation); len += 4; /* CreateDisposition */
862 /* 0x30 */
863 SMB_ADDDWORD(&buffer[len],creation); len += 4; /* CreateOptions */
865 /* 0x34 */
866 SMB_ADDDWORD(&buffer[len],0); len += 4; /* Impersonation */
868 /* 0x38 */
869 buffer[len++] = 0; /* security flags */
871 /* 0x39 */
872 SMB_ADDWORD(&buffer[len],slen); len += 2; /* size of buffer */
874 if(slen<(sizeof(buffer)-len))
875 strcpy(&buffer[len], filename);
876 else
877 return FALSE;
878 len += slen+1;
880 /* name of the file */
881 buffer[len++] = 0;
883 if(!NB_Transaction(fd, buffer, len, &len))
884 return FALSE;
886 if(SMB_GetError(buffer))
887 return FALSE;
889 TRACE("OK\n");
891 /* FIXME */
892 /* *file_id = SMB_GETWORD(&buffer[xxx]); */
893 *file_id = 0;
894 return FALSE;
896 return TRUE;
898 #endif
900 static USHORT SMB_GetMode(DWORD access, DWORD sharing)
902 USHORT mode=0;
904 switch(access&(GENERIC_READ|GENERIC_WRITE))
906 case GENERIC_READ:
907 mode |= OF_READ;
908 break;
909 case GENERIC_WRITE:
910 mode |= OF_WRITE;
911 break;
912 case (GENERIC_READ|GENERIC_WRITE):
913 mode |= OF_READWRITE;
914 break;
917 switch(sharing&(FILE_SHARE_READ|FILE_SHARE_WRITE))
919 case (FILE_SHARE_READ|FILE_SHARE_WRITE):
920 mode |= OF_SHARE_DENY_NONE;
921 break;
922 case FILE_SHARE_READ:
923 mode |= OF_SHARE_DENY_WRITE;
924 break;
925 case FILE_SHARE_WRITE:
926 mode |= OF_SHARE_DENY_READ;
927 break;
928 default:
929 mode |= OF_SHARE_EXCLUSIVE;
930 break;
933 return mode;
936 #if 0 /* not yet */
937 /* inverse of FILE_ConvertOFMode */
938 static BOOL SMB_OpenAndX(int fd, USHORT tree_id, USHORT user_id, USHORT dialect,
939 LPCSTR filename, DWORD access, DWORD sharing,
940 DWORD creation, DWORD attributes, USHORT *file_id )
942 unsigned char buffer[0x100];
943 int len = 0;
944 USHORT mode;
946 TRACE("%s\n",filename);
948 mode = SMB_GetMode(access,sharing);
950 memset(buffer,0,sizeof(buffer));
952 len = SMB_Header(buffer, SMB_COM_OPEN_ANDX, tree_id, user_id);
954 /* 0 */
955 buffer[len++] = 15; /* parameters */
956 buffer[len++] = 0xff; /* AndXCommand: secondary request */
957 buffer[len++] = 0x00; /* AndXReserved */
958 SMB_ADDWORD(buffer+len,0); len+=2; /* AndXOffset */
959 SMB_ADDWORD(buffer+len,0); len+=2; /* Flags */
960 SMB_ADDWORD(buffer+len,mode); len+=2; /* desired access */
961 SMB_ADDWORD(buffer+len,0); len+=2; /* search attributes */
962 SMB_ADDWORD(buffer+len,0); len+=2;
964 /*FIXME: complete */
965 return FALSE;
967 #endif
970 static BOOL SMB_Open(int fd, USHORT tree_id, USHORT user_id, USHORT dialect,
971 LPCSTR filename, DWORD access, DWORD sharing,
972 DWORD creation, DWORD attributes, USHORT *file_id )
974 unsigned char buf[0x100];
975 int slen,pcount,i;
976 USHORT mode = SMB_GetMode(access,sharing);
977 struct NB_Buffer rx,tx;
979 TRACE("%s\n",filename);
981 memset(buf,0,sizeof(buf));
983 tx.buffer = buf;
984 tx.len = SMB_Header(tx.buffer, SMB_COM_OPEN, tree_id, user_id);
986 /* 0 */
987 tx.buffer[tx.len++] = 2; /* parameters */
988 SMB_ADDWORD(tx.buffer+tx.len,mode); tx.len+=2;
989 SMB_ADDWORD(tx.buffer+tx.len,0); tx.len+=2; /* search attributes */
991 slen = strlen(filename)+2; /* inc. nul and BufferFormat */
992 SMB_ADDWORD(tx.buffer+tx.len,slen); tx.len+=2;
994 tx.buffer[tx.len] = 0x04; /* BufferFormat */
995 strcpy(&tx.buffer[tx.len+1],filename);
996 tx.len += slen;
998 rx.buffer = NULL;
999 rx.len = 0;
1000 if(!NB_Transaction(fd, &tx, &rx))
1001 return FALSE;
1003 if(!rx.buffer)
1004 return FALSE;
1006 if(SMB_GetError(rx.buffer))
1007 return FALSE;
1009 pcount = SMB_PARAM_COUNT(rx.buffer);
1011 if( (SMB_HDRSIZE+pcount*2) > rx.len )
1013 ERR("Bad parameter count %d\n",pcount);
1014 return FALSE;
1017 TRACE("response, %d args: ",pcount);
1018 for(i=0; i<pcount; i++)
1019 TRACE("%04x ",SMB_PARAM(rx.buffer,i));
1020 TRACE("\n");
1022 *file_id = SMB_PARAM(rx.buffer,0);
1024 TRACE("file_id = %04x\n",*file_id);
1026 return TRUE;
1030 static BOOL SMB_Read(int fd, USHORT tree_id, USHORT user_id, USHORT dialect,
1031 USHORT file_id, DWORD offset, LPVOID out, USHORT count, USHORT* read)
1033 int buf_size,n,i;
1034 struct NB_Buffer rx,tx;
1036 TRACE("user %04x tree %04x file %04x count %04x offset %08lx\n",
1037 user_id, tree_id, file_id, count, offset);
1039 buf_size = count+0x100;
1040 tx.buffer = (unsigned char *) RtlAllocateHeap(GetProcessHeap(),0,buf_size);
1042 memset(tx.buffer,0,buf_size);
1044 tx.len = SMB_Header(tx.buffer, SMB_COM_READ, tree_id, user_id);
1046 tx.buffer[tx.len++] = 5;
1047 SMB_ADDWORD(&tx.buffer[tx.len],file_id); tx.len += 2;
1048 SMB_ADDWORD(&tx.buffer[tx.len],count); tx.len += 2;
1049 SMB_ADDDWORD(&tx.buffer[tx.len],offset); tx.len += 4;
1050 SMB_ADDWORD(&tx.buffer[tx.len],0); tx.len += 2; /* how many more bytes will be read */
1052 tx.buffer[tx.len++] = 0;
1054 rx.buffer = NULL;
1055 rx.len = 0;
1056 if(!NB_Transaction(fd, &tx, &rx))
1058 RtlFreeHeap(GetProcessHeap(),0,tx.buffer);
1059 return FALSE;
1062 if(SMB_GetError(rx.buffer))
1064 RtlFreeHeap(GetProcessHeap(),0,rx.buffer);
1065 RtlFreeHeap(GetProcessHeap(),0,tx.buffer);
1066 return FALSE;
1069 n = SMB_PARAM_COUNT(rx.buffer);
1071 if( (SMB_HDRSIZE+n*2) > rx.len )
1073 RtlFreeHeap(GetProcessHeap(),0,rx.buffer);
1074 RtlFreeHeap(GetProcessHeap(),0,tx.buffer);
1075 ERR("Bad parameter count %d\n",n);
1076 return FALSE;
1079 TRACE("response, %d args: ",n);
1080 for(i=0; i<n; i++)
1081 TRACE("%04x ",SMB_PARAM(rx.buffer,i));
1082 TRACE("\n");
1084 n = SMB_PARAM(rx.buffer,5) - 3;
1085 if(n>count)
1086 n=count;
1088 memcpy( out, &SMB_BUFFER(rx.buffer,3), n);
1090 TRACE("Read %d bytes\n",n);
1091 *read = n;
1093 RtlFreeHeap(GetProcessHeap(),0,tx.buffer);
1094 RtlFreeHeap(GetProcessHeap(),0,rx.buffer);
1096 return TRUE;
1101 * setup_count : number of USHORTs in the setup string
1103 struct SMB_Trans2Info
1105 struct NB_Buffer buf;
1106 unsigned char *setup;
1107 int setup_count;
1108 unsigned char *params;
1109 int param_count;
1110 unsigned char *data;
1111 int data_count;
1115 * Do an SMB transaction
1117 * This function allocates memory in the recv structure. It is
1118 * the caller's responsibility to free the memory if it finds
1119 * that recv->buf.buffer is nonzero.
1121 static BOOL SMB_Transaction2(int fd, int tree_id, int user_id,
1122 struct SMB_Trans2Info *send,
1123 struct SMB_Trans2Info *recv)
1125 int buf_size;
1126 const int retmaxparams = 0xf000;
1127 const int retmaxdata = 1024;
1128 const int retmaxsetup = 0; /* FIXME */
1129 const int flags = 0;
1130 const int timeout = 0;
1131 int param_ofs, data_ofs;
1132 struct NB_Buffer tx;
1133 BOOL ret = FALSE;
1135 buf_size = 0x100 + send->setup_count*2 + send->param_count + send->data_count ;
1136 tx.buffer = (unsigned char *) RtlAllocateHeap(GetProcessHeap(),0,buf_size);
1138 tx.len = SMB_Header(tx.buffer, SMB_COM_TRANSACTION2, tree_id, user_id);
1140 tx.buffer[tx.len++] = 14 + send->setup_count;
1141 SMB_ADDWORD(&tx.buffer[tx.len],send->param_count); /* total param bytes sent */
1142 tx.len += 2;
1143 SMB_ADDWORD(&tx.buffer[tx.len],send->data_count); /* total data bytes sent */
1144 tx.len += 2;
1145 SMB_ADDWORD(&tx.buffer[tx.len],retmaxparams); /*max parameter bytes to return */
1146 tx.len += 2;
1147 SMB_ADDWORD(&tx.buffer[tx.len],retmaxdata); /* max data bytes to return */
1148 tx.len += 2;
1149 tx.buffer[tx.len++] = retmaxsetup;
1150 tx.buffer[tx.len++] = 0; /* reserved1 */
1152 SMB_ADDWORD(&tx.buffer[tx.len],flags); /* flags */
1153 tx.len += 2;
1154 SMB_ADDDWORD(&tx.buffer[tx.len],timeout); /* timeout */
1155 tx.len += 4;
1156 SMB_ADDWORD(&tx.buffer[tx.len],0); /* reserved2 */
1157 tx.len += 2;
1158 SMB_ADDWORD(&tx.buffer[tx.len],send->param_count); /* parameter count - this buffer */
1159 tx.len += 2;
1161 param_ofs = tx.len; /* parameter offset */
1162 tx.len += 2;
1163 SMB_ADDWORD(&tx.buffer[tx.len],send->data_count); /* data count */
1164 tx.len += 2;
1166 data_ofs = tx.len; /* data offset */
1167 tx.len += 2;
1168 tx.buffer[tx.len++] = send->setup_count; /* setup count */
1169 tx.buffer[tx.len++] = 0; /* reserved3 */
1171 memcpy(&tx.buffer[tx.len], send->setup, send->setup_count*2); /* setup */
1172 tx.len += send->setup_count*2;
1174 /* add string here when implementing SMB_COM_TRANS */
1176 SMB_ADDWORD(&tx.buffer[param_ofs], tx.len);
1177 memcpy(&tx.buffer[tx.len], send->params, send->param_count); /* parameters */
1178 tx.len += send->param_count;
1179 if(tx.len%2)
1180 tx.len ++; /* pad2 */
1182 SMB_ADDWORD(&tx.buffer[data_ofs], tx.len);
1183 if(send->data_count && send->data)
1185 memcpy(&tx.buffer[tx.len], send->data, send->data_count); /* data */
1186 tx.len += send->data_count;
1189 recv->buf.buffer = NULL;
1190 recv->buf.len = 0;
1191 if(!NB_Transaction(fd, &tx, &recv->buf))
1192 goto done;
1194 if(!recv->buf.buffer)
1195 goto done;
1197 if(SMB_GetError(recv->buf.buffer))
1198 goto done;
1200 /* reuse these two offsets to check the received message */
1201 param_ofs = SMB_PARAM(recv->buf.buffer,4);
1202 data_ofs = SMB_PARAM(recv->buf.buffer,7);
1204 if( (recv->param_count + param_ofs) > recv->buf.len )
1205 goto done;
1207 if( (recv->data_count + data_ofs) > recv->buf.len )
1208 goto done;
1210 TRACE("Success\n");
1212 recv->setup = NULL;
1213 recv->setup_count = 0;
1215 recv->param_count = SMB_PARAM(recv->buf.buffer,0);
1216 recv->params = &recv->buf.buffer[param_ofs];
1218 recv->data_count = SMB_PARAM(recv->buf.buffer,6);
1219 recv->data = &recv->buf.buffer[data_ofs];
1222 TRACE("%d words\n",SMB_PARAM_COUNT(recv->buf.buffer));
1223 TRACE("total parameters = %d\n",SMB_PARAM(recv->buf.buffer,0));
1224 TRACE("total data = %d\n",SMB_PARAM(recv->buf.buffer,1));
1225 TRACE("parameters = %d\n",SMB_PARAM(recv->buf.buffer,3));
1226 TRACE("parameter offset = %d\n",SMB_PARAM(recv->buf.buffer,4));
1227 TRACE("param displace = %d\n",SMB_PARAM(recv->buf.buffer,5));
1229 TRACE("data count = %d\n",SMB_PARAM(recv->buf.buffer,6));
1230 TRACE("data offset = %d\n",SMB_PARAM(recv->buf.buffer,7));
1231 TRACE("data displace = %d\n",SMB_PARAM(recv->buf.buffer,8));
1234 ret = TRUE;
1236 done:
1237 if(tx.buffer)
1238 RtlFreeHeap(GetProcessHeap(),0,tx.buffer);
1240 return ret;
1243 static BOOL SMB_SetupFindFirst(struct SMB_Trans2Info *send, LPSTR filename)
1245 int search_attribs = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
1246 int search_count = 10;
1247 int flags = 0;
1248 int infolevel = 0x104; /* SMB_FILE_BOTH_DIRECTORY_INFO */
1249 int storagetype = 0;
1250 int len, buf_size;
1252 memset(send,0,sizeof(send));
1254 send->setup_count = 1;
1255 send->setup = RtlAllocateHeap(GetProcessHeap(),0,send->setup_count*2);
1256 if(!send->setup)
1257 return FALSE;
1259 buf_size = 0x10 + strlen(filename);
1260 send->params = RtlAllocateHeap(GetProcessHeap(),0,buf_size);
1261 if(!send->params)
1263 RtlFreeHeap(GetProcessHeap(),0,send->setup);
1264 return FALSE;
1267 SMB_ADDWORD(send->setup,TRANS2_FIND_FIRST2);
1269 len = 0;
1270 memset(send->params,0,buf_size);
1271 SMB_ADDWORD(&send->params[len],search_attribs); len += 2;
1272 SMB_ADDWORD(&send->params[len],search_count); len += 2;
1273 SMB_ADDWORD(&send->params[len],flags); len += 2;
1274 SMB_ADDWORD(&send->params[len],infolevel); len += 2;
1275 SMB_ADDDWORD(&send->params[len],storagetype); len += 4;
1277 strcpy(&send->params[len],filename);
1278 len += strlen(filename)+1;
1280 send->param_count = len;
1281 send->data = NULL;
1282 send->data_count = 0;
1284 return TRUE;
1287 static SMB_DIR *SMB_Trans2FindFirst(int fd, USHORT tree_id,
1288 USHORT user_id, USHORT dialect, LPSTR filename )
1290 int num;
1291 BOOL ret;
1292 /* char *filename = "\\*"; */
1293 struct SMB_Trans2Info send, recv;
1294 SMB_DIR *smbdir = NULL;
1296 TRACE("pattern = %s\n",filename);
1298 if(!SMB_SetupFindFirst(&send, filename))
1299 return FALSE;
1301 memset(&recv,0,sizeof(recv));
1303 ret = SMB_Transaction2(fd, tree_id, user_id, &send, &recv);
1304 RtlFreeHeap(GetProcessHeap(),0,send.params);
1305 RtlFreeHeap(GetProcessHeap(),0,send.setup);
1307 if(!ret)
1308 goto done;
1310 if(recv.setup_count)
1311 goto done;
1313 if(recv.param_count != 10)
1314 goto done;
1316 num = SMB_GETWORD(&recv.params[2]);
1317 TRACE("Success, search id: %d\n",num);
1319 if(SMB_GETWORD(&recv.params[4]))
1320 FIXME("need to read more!\n");
1322 smbdir = RtlAllocateHeap(GetProcessHeap(),0,sizeof(*smbdir));
1323 if(smbdir)
1325 int i, ofs=0;
1327 smbdir->current = 0;
1328 smbdir->num_entries = num;
1329 smbdir->entries = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(unsigned char*)*num);
1330 if(!smbdir->entries)
1331 goto done;
1332 smbdir->buffer = recv.buf.buffer; /* save to free later */
1334 for(i=0; i<num; i++)
1336 int size = SMB_GETDWORD(&recv.data[ofs]);
1338 smbdir->entries[i] = &recv.data[ofs];
1340 if(TRACE_ON(file))
1342 int j;
1343 for(j=0; j<size; j++)
1344 DPRINTF("%02x%c",recv.data[ofs+j],((j+1)%16)?' ':'\n');
1346 TRACE("file %d : %s\n", i, &recv.data[ofs+0x5e]);
1347 ofs += size;
1348 if(ofs>recv.data_count)
1349 goto done;
1352 ret = TRUE;
1355 done:
1356 if(!ret)
1358 if( recv.buf.buffer )
1359 RtlFreeHeap(GetProcessHeap(),0,recv.buf.buffer);
1360 if( smbdir )
1362 if( smbdir->entries )
1363 RtlFreeHeap(GetProcessHeap(),0,smbdir->entries);
1364 RtlFreeHeap(GetProcessHeap(),0,smbdir);
1366 smbdir = NULL;
1369 return smbdir;
1372 static int SMB_GetSocket(LPCSTR host)
1374 int fd=-1,r;
1375 struct sockaddr_in sin;
1376 struct hostent *he;
1378 TRACE("host %s\n",host);
1380 he = gethostbyname(host);
1381 if(he)
1383 memcpy(&sin.sin_addr,he->h_addr, sizeof (sin.sin_addr));
1384 goto connect;
1387 if(NB_Lookup(host,&sin))
1388 goto connect;
1390 /* FIXME: resolve by WINS too */
1392 ERR("couldn't resolve SMB host %s\n", host);
1394 return -1;
1396 connect:
1397 sin.sin_family = AF_INET;
1398 sin.sin_port = htons(139); /* netbios session */
1400 fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
1401 if(fd<0)
1402 return fd;
1405 unsigned char *x = (unsigned char *)&sin.sin_addr;
1406 TRACE("Connecting to %d.%d.%d.%d ...\n", x[0],x[1],x[2],x[3]);
1408 r = connect(fd, (struct sockaddr*)&sin, sizeof(sin));
1410 if(!NB_SessionReq(fd, "*SMBSERVER", "WINE"))
1412 close(fd);
1413 return -1;
1416 return fd;
1419 static BOOL SMB_LoginAndConnect(int fd, LPCSTR host, LPCSTR share, USHORT *tree_id, USHORT *user_id, USHORT *dialect)
1421 LPSTR name=NULL;
1423 TRACE("host %s share %s\n",host,share);
1425 if(!SMB_NegotiateProtocol(fd, dialect))
1426 return FALSE;
1428 if(!SMB_SessionSetup(fd, user_id))
1429 return FALSE;
1431 name = RtlAllocateHeap(GetProcessHeap(),0,strlen(host)+strlen(share)+5);
1432 if(!name)
1433 return FALSE;
1435 sprintf(name,"\\\\%s\\%s",host,share);
1436 if(!SMB_TreeConnect(fd,*user_id,name,tree_id))
1438 RtlFreeHeap(GetProcessHeap(),0,name);
1439 return FALSE;
1442 return TRUE;
1445 static HANDLE SMB_RegisterFile( int fd, USHORT tree_id, USHORT user_id, USHORT dialect, USHORT file_id)
1447 int r;
1448 HANDLE ret;
1450 wine_server_send_fd( fd );
1452 SERVER_START_REQ( create_smb )
1454 req->tree_id = tree_id;
1455 req->user_id = user_id;
1456 req->file_id = file_id;
1457 req->dialect = 0;
1458 req->fd = fd;
1459 SetLastError(0);
1460 r = wine_server_call_err( req );
1461 ret = reply->handle;
1463 SERVER_END_REQ;
1465 if(!r)
1466 TRACE("created wineserver smb object, handle = %p\n",ret);
1467 else
1468 SetLastError( ERROR_PATH_NOT_FOUND );
1470 return ret;
1473 HANDLE WINAPI SMB_CreateFileW( LPCWSTR uncname, DWORD access, DWORD sharing,
1474 LPSECURITY_ATTRIBUTES sa, DWORD creation,
1475 DWORD attributes, HANDLE template )
1477 int fd;
1478 USHORT tree_id=0, user_id=0, dialect=0, file_id=0;
1479 LPSTR name,host,share,file;
1480 HANDLE handle = INVALID_HANDLE_VALUE;
1481 INT len;
1483 len = WideCharToMultiByte(CP_ACP, 0, uncname, -1, NULL, 0, NULL, NULL);
1484 name = RtlAllocateHeap(GetProcessHeap(), 0, len);
1485 if(!name)
1486 return handle;
1488 WideCharToMultiByte(CP_ACP, 0, uncname, -1, name, len, NULL, NULL);
1490 if( !UNC_SplitName(name, &host, &share, &file) )
1492 RtlFreeHeap(GetProcessHeap(),0,name);
1493 return handle;
1496 TRACE("server is %s, share is %s, file is %s\n", host, share, file);
1498 fd = SMB_GetSocket(host);
1499 if(fd < 0)
1500 goto done;
1502 if(!SMB_LoginAndConnect(fd, host, share, &tree_id, &user_id, &dialect))
1503 goto done;
1505 #if 0
1506 if(!SMB_NtCreateOpen(fd, tree_id, user_id, dialect, file,
1507 access, sharing, sa, creation, attributes, template, &file_id ))
1509 close(fd);
1510 ERR("CreateOpen failed\n");
1511 goto done;
1513 #endif
1514 if(!SMB_Open(fd, tree_id, user_id, dialect, file,
1515 access, sharing, creation, attributes, &file_id ))
1517 close(fd);
1518 ERR("CreateOpen failed\n");
1519 goto done;
1522 handle = SMB_RegisterFile(fd, tree_id, user_id, dialect, file_id);
1523 if(!handle)
1525 ERR("register failed\n");
1526 close(fd);
1529 done:
1530 RtlFreeHeap(GetProcessHeap(),0,name);
1531 return handle;
1534 static NTSTATUS SMB_GetSmbInfo(HANDLE hFile, USHORT *tree_id, USHORT *user_id, USHORT *dialect, USHORT *file_id, LPDWORD offset)
1536 NTSTATUS status;
1538 SERVER_START_REQ( get_smb_info )
1540 req->handle = hFile;
1541 req->flags = 0;
1542 status = wine_server_call( req );
1543 if(tree_id)
1544 *tree_id = reply->tree_id;
1545 if(user_id)
1546 *user_id = reply->user_id;
1547 if(file_id)
1548 *file_id = reply->file_id;
1549 if(dialect)
1550 *dialect = reply->dialect;
1551 if(offset)
1552 *offset = reply->offset;
1554 SERVER_END_REQ;
1556 return status;
1559 static NTSTATUS SMB_SetOffset(HANDLE hFile, DWORD offset)
1561 NTSTATUS status;
1563 TRACE("offset = %08lx\n",offset);
1565 SERVER_START_REQ( get_smb_info )
1567 req->handle = hFile;
1568 req->flags = SMBINFO_SET_OFFSET;
1569 req->offset = offset;
1570 status = wine_server_call( req );
1571 /* if(offset)
1572 *offset = reply->offset; */
1574 SERVER_END_REQ;
1576 return status;
1579 NTSTATUS WINAPI SMB_ReadFile(HANDLE hFile, int fd, LPVOID buffer, DWORD bytesToRead,
1580 PIO_STATUS_BLOCK io_status)
1582 DWORD count, offset;
1583 USHORT user_id, tree_id, dialect, file_id, read;
1585 TRACE("%p %p %ld %p\n", hFile, buffer, bytesToRead, io_status);
1587 io_status->Information = 0;
1589 io_status->u.Status = SMB_GetSmbInfo(hFile, &tree_id, &user_id, &dialect, &file_id, &offset);
1590 if (io_status->u.Status) return io_status->u.Status;
1592 while(1)
1594 count = bytesToRead - io_status->Information;
1595 if(count>0x400)
1596 count = 0x400;
1597 if(count==0)
1598 break;
1599 read = 0;
1600 if (!SMB_Read(fd, tree_id, user_id, dialect, file_id, offset, buffer, count, &read))
1601 break;
1602 if(!read)
1603 break;
1604 io_status->Information += read;
1605 buffer = (char*)buffer + read;
1606 offset += read;
1607 if(io_status->Information >= bytesToRead)
1608 break;
1610 return io_status->u.Status = SMB_SetOffset(hFile, offset);
1613 SMB_DIR* WINAPI SMB_FindFirst(LPCWSTR name)
1615 int fd = -1;
1616 LPSTR host,share,file;
1617 USHORT tree_id=0, user_id=0, dialect=0;
1618 SMB_DIR *ret = NULL;
1619 LPSTR filename;
1620 DWORD len;
1622 TRACE("Find %s\n",debugstr_w(name));
1624 len = WideCharToMultiByte( CP_ACP, 0, name, -1, NULL, 0, NULL, NULL );
1625 filename = RtlAllocateHeap(GetProcessHeap(),0,len);
1626 if(!filename)
1627 return ret;
1628 WideCharToMultiByte( CP_ACP, 0, name, -1, filename, len, NULL, NULL );
1630 if( !UNC_SplitName(filename, &host, &share, &file) )
1631 goto done;
1633 fd = SMB_GetSocket(host);
1634 if(fd < 0)
1635 goto done;
1637 if(!SMB_LoginAndConnect(fd, host, share, &tree_id, &user_id, &dialect))
1638 goto done;
1640 TRACE("server is %s, share is %s, file is %s\n", host, share, file);
1642 ret = SMB_Trans2FindFirst(fd, tree_id, user_id, dialect, file);
1644 done:
1645 /* disconnect */
1646 if(fd != -1)
1647 close(fd);
1649 if(filename)
1650 RtlFreeHeap(GetProcessHeap(),0,filename);
1652 return ret;
1656 BOOL WINAPI SMB_FindNext(SMB_DIR *dir, WIN32_FIND_DATAW *data )
1658 unsigned char *ent;
1659 int len, fnlen;
1661 TRACE("%d of %d\n",dir->current,dir->num_entries);
1663 if(dir->current >= dir->num_entries)
1664 return FALSE;
1666 memset(data, 0, sizeof(*data));
1668 ent = dir->entries[dir->current];
1669 len = SMB_GETDWORD(&ent[0]);
1670 if(len<0x5e)
1671 return FALSE;
1673 memcpy(&data->ftCreationTime, &ent[8], 8);
1674 memcpy(&data->ftLastAccessTime, &ent[0x10], 8);
1675 memcpy(&data->ftLastWriteTime, &ent[0x18], 8);
1676 data->nFileSizeHigh = SMB_GETDWORD(&ent[0x30]);
1677 data->nFileSizeLow = SMB_GETDWORD(&ent[0x34]);
1678 data->dwFileAttributes = SMB_GETDWORD(&ent[0x38]);
1680 /* copy the long filename */
1681 fnlen = SMB_GETDWORD(&ent[0x3c]);
1682 if ( fnlen > (sizeof(data->cFileName)/sizeof(WCHAR)) )
1683 return FALSE;
1684 MultiByteToWideChar( CP_ACP, 0, &ent[0x5e], fnlen, data->cFileName,
1685 sizeof(data->cFileName)/sizeof(WCHAR) );
1687 /* copy the short filename */
1688 if ( ent[0x44] > (sizeof(data->cAlternateFileName)/sizeof(WCHAR)) )
1689 return FALSE;
1690 MultiByteToWideChar( CP_ACP, 0, &ent[0x5e + len], ent[0x44], data->cAlternateFileName,
1691 sizeof(data->cAlternateFileName)/sizeof(WCHAR) );
1693 dir->current++;
1695 return TRUE;
1698 BOOL WINAPI SMB_CloseDir(SMB_DIR *dir)
1700 RtlFreeHeap(GetProcessHeap(),0,dir->buffer);
1701 RtlFreeHeap(GetProcessHeap(),0,dir->entries);
1702 memset(dir,0,sizeof(*dir));
1703 RtlFreeHeap(GetProcessHeap(),0,dir);
1704 return TRUE;