1 /****************************************************************************
3 * Nintendo Wii/GameCube SMB implementation
6 * Modified by Tantric to utilize NTLM authentication
7 * PathInfo added by rodries
8 * SMB devoptab by scip, rodries
10 * You will find WireShark (http://www.wireshark.org/)
11 * invaluable for debugging SAMBA implementations.
14 * Implementing CIFS - Christopher R Hertel
15 * http://www.ubiqx.org/cifs/SMB.html
19 * This library is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU Lesser General Public
21 * License as published by the Free Software Foundation; either
22 * version 2.1 of the License, or (at your option) any later version.
24 * This library is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * Lesser General Public License for more details.
29 * You should have received a copy of the GNU Lesser General Public
30 * License along with this library; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 ****************************************************************************/
44 #include <processor.h>
45 #include <lwp_threads.h>
46 #include <lwp_objmgr.h>
47 #include <ogc/lwp_watchdog.h>
48 #include <sys/statvfs.h>
53 #define IOS_O_NONBLOCK 0x04
54 #define RECV_TIMEOUT 3000 // in ms
55 #define CONN_TIMEOUT 6000
60 #define SMB_OFFSET_PROTO 0
61 #define SMB_OFFSET_CMD 4
62 #define SMB_OFFSET_NTSTATUS 5
63 #define SMB_OFFSET_ECLASS 5
64 #define SMB_OFFSET_ECODE 7
65 #define SMB_OFFSET_FLAGS 9
66 #define SMB_OFFSET_FLAGS2 10
67 #define SMB_OFFSET_EXTRA 12
68 #define SMB_OFFSET_TID 24
69 #define SMB_OFFSET_PID 26
70 #define SMB_OFFSET_UID 28
71 #define SMB_OFFSET_MID 30
72 #define SMB_HEADER_SIZE 32 /*** SMB Headers are always 32 bytes long ***/
77 #define NBT_SESSISON_MSG 0x00
79 #define SMB_NEG_PROTOCOL 0x72
80 #define SMB_SETUP_ANDX 0x73
81 #define SMB_TREEC_ANDX 0x75
84 #define NBT_KEEPALIVE_MSG 0x85
85 #define KEEPALIVE_SIZE 4
90 #define SMB_TRANS2 0x32
93 #define SMB_FIND_FIRST2 1
94 #define SMB_FIND_NEXT2 2
95 #define SMB_QUERY_FS_INFO 3
96 #define SMB_QUERY_PATH_INFO 5
97 #define SMB_SET_PATH_INFO 6
98 #define SMB_QUERY_FILE_INFO 7
99 #define SMB_SET_FILE_INFO 8
100 #define SMB_CREATE_DIR 13
101 #define SMB_FIND_CLOSE2 0x34
102 #define SMB_QUERY_FILE_ALL_INFO 0x107
107 #define SMB_OPEN_ANDX 0x2d
108 #define SMB_WRITE_ANDX 0x2f
109 #define SMB_READ_ANDX 0x2e
110 #define SMB_CLOSE 0x04
115 #define SMB_COM_CREATE_DIRECTORY 0x00
116 #define SMB_COM_DELETE_DIRECTORY 0x01
117 #define SMB_COM_DELETE 0x06
118 #define SMB_COM_RENAME 0x07
119 #define SMB_COM_QUERY_INFORMATION_DISK 0x80
124 #define T2_WORD_CNT (SMB_HEADER_SIZE)
125 #define T2_PRM_CNT (T2_WORD_CNT+1)
126 #define T2_DATA_CNT (T2_PRM_CNT+2)
127 #define T2_MAXPRM_CNT (T2_DATA_CNT+2)
128 #define T2_MAXBUFFER (T2_MAXPRM_CNT+2)
129 #define T2_SETUP_CNT (T2_MAXBUFFER+2)
130 #define T2_SPRM_CNT (T2_SETUP_CNT+10)
131 #define T2_SPRM_OFS (T2_SPRM_CNT+2)
132 #define T2_SDATA_CNT (T2_SPRM_OFS+2)
133 #define T2_SDATA_OFS (T2_SDATA_CNT+2)
134 #define T2_SSETUP_CNT (T2_SDATA_OFS+2)
135 #define T2_SUB_CMD (T2_SSETUP_CNT+2)
136 #define T2_BYTE_CNT (T2_SUB_CMD+2)
139 #define SMB_PROTO 0x424d53ff
140 #define SMB_HANDLE_NULL 0xffffffff
141 #define SMB_MAX_NET_READ_SIZE (16*1024) // see smb_recv
142 #define SMB_MAX_NET_WRITE_SIZE 4096 // see smb_sendv
143 #define SMB_MAX_TRANSMIT_SIZE 65472
145 #define CAP_LARGE_FILES 0x00000008 // 64-bit file sizes and offsets supported
146 #define CAP_UNICODE 0x00000004 // Unicode supported
147 #define CIFS_FLAGS1 0x08 // Paths are caseless
148 #define CIFS_FLAGS2_UNICODE 0x8001 // Server may return long components in paths in the response - use 0x8001 for Unicode support
149 #define CIFS_FLAGS2 0x0001 // Server may return long components in paths in the response - use 0x0001 for ASCII support
151 #define SMB_CONNHANDLES_MAX 8
152 #define SMB_FILEHANDLES_MAX (32*SMB_CONNHANDLES_MAX)
154 #define SMB_OBJTYPE_HANDLE 7
155 #define SMB_CHECK_HANDLE(hndl) \
157 if(((hndl)==SMB_HANDLE_NULL) || (LWP_OBJTYPE(hndl)!=SMB_OBJTYPE_HANDLE)) \
161 /* NBT Session Service Packet Type Codes
164 #define SESS_MSG 0x00
165 #define SESS_REQ 0x81
166 #define SESS_POS_RESP 0x82
167 #define SESS_NEG_RESP 0x83
168 #define SESS_RETARGET 0x84
169 #define SESS_KEEPALIVE 0x85
181 typedef struct _nbtsmb
183 u8 msg
; /*** NBT Message ***/
185 u16 length
; /*** Length, excluding NBT ***/
186 u8 smb
[SMB_MAX_TRANSMIT_SIZE
+128];
190 * Session Information
192 typedef struct _smbsession
212 typedef struct _smbhandle
220 struct sockaddr_in server_addr
;
227 static u32 smb_dialectcnt
= 1;
228 static bool smb_inited
= false;
229 static lwp_objinfo smb_handle_objects
;
230 static lwp_queue smb_filehandle_queue
;
231 static struct _smbfile smb_filehandles
[SMB_FILEHANDLES_MAX
];
232 static const char *smb_dialects
[] = {"NT LM 0.12",NULL
};
234 extern void ntlm_smb_nt_encrypt(const char *passwd
, const u8
* challenge
, u8
* answer
);
236 // UTF conversion functions
237 size_t utf16_to_utf8(char* dst
, char* src
, size_t len
)
242 char buff
[MB_CUR_MAX
];
245 memset(&ps
, 0, sizeof(mbstate_t));
247 while (count
< len
&& *src
!= '\0')
249 c
= *(src
+ 1) << 8 | *src
; // little endian
252 bytes
= wcrtomb(buff
, c
, &ps
);
260 for (i
= 0; i
< bytes
; i
++)
276 size_t utf8_to_utf16(char* dst
, char* src
, size_t len
)
283 tempChar
= (char*) &tempWChar
;
284 memset(&ps
, 0, sizeof(mbstate_t));
286 while (count
< len
- 1 && src
!= '\0')
288 bytes
= mbrtowc(&tempWChar
, src
, MB_CUR_MAX
, &ps
);
317 * SMB Endian aware supporting functions
319 * SMB always uses Intel Little-Endian values, so htons etc are
320 * of little or no use :) ... Thanks M$
323 /*** get unsigned char ***/
324 static __inline__ u8
getUChar(u8
*buffer
,u32 offset
)
326 return (u8
)buffer
[offset
];
329 /*** set unsigned char ***/
330 static __inline__
void setUChar(u8
*buffer
,u32 offset
,u8 value
)
332 buffer
[offset
] = value
;
335 /*** get signed short ***/
336 static __inline__ s16
getShort(u8
*buffer
,u32 offset
)
338 return (s16
)((buffer
[offset
+1]<<8)|(buffer
[offset
]));
341 /*** get unsigned short ***/
342 static __inline__ u16
getUShort(u8
*buffer
,u32 offset
)
344 return (u16
)((buffer
[offset
+1]<<8)|(buffer
[offset
]));
347 /*** set unsigned short ***/
348 static __inline__
void setUShort(u8
*buffer
,u32 offset
,u16 value
)
350 buffer
[offset
] = (value
&0xff);
351 buffer
[offset
+1] = ((value
&0xff00)>>8);
354 /*** get unsigned int ***/
355 static __inline__ u32
getUInt(u8
*buffer
,u32 offset
)
357 return (u32
)((buffer
[offset
+3]<<24)|(buffer
[offset
+2]<<16)|(buffer
[offset
+1]<<8)|buffer
[offset
]);
360 /*** set unsigned int ***/
361 static __inline__
void setUInt(u8
*buffer
,u32 offset
,u32 value
)
363 buffer
[offset
] = (value
&0xff);
364 buffer
[offset
+1] = ((value
&0xff00)>>8);
365 buffer
[offset
+2] = ((value
&0xff0000)>>16);
366 buffer
[offset
+3] = ((value
&0xff000000)>>24);
369 /*** get unsigned long long ***/
370 static __inline__ u64
getULongLong(u8
*buffer
,u32 offset
)
372 return (u64
)(getUInt(buffer
, offset
) | (u64
)getUInt(buffer
, offset
+4) << 32);
375 static __inline__ SMBHANDLE
* __smb_handle_open(SMBCONN smbhndl
)
380 SMB_CHECK_HANDLE(smbhndl
);
382 _CPU_ISR_Disable(level
);
383 handle
= (SMBHANDLE
*)__lwp_objmgr_getnoprotection(&smb_handle_objects
,LWP_OBJMASKID(smbhndl
));
384 _CPU_ISR_Restore(level
);
389 static __inline__
void __smb_handle_free(SMBHANDLE
*handle
)
393 _CPU_ISR_Disable(level
);
394 __lwp_objmgr_close(&smb_handle_objects
,&handle
->object
);
395 __lwp_objmgr_free(&smb_handle_objects
,&handle
->object
);
396 _CPU_ISR_Restore(level
);
399 static void __smb_init()
402 __lwp_objmgr_initinfo(&smb_handle_objects
,SMB_CONNHANDLES_MAX
,sizeof(SMBHANDLE
));
403 __lwp_queue_initialize(&smb_filehandle_queue
,smb_filehandles
,SMB_FILEHANDLES_MAX
,sizeof(struct _smbfile
));
406 static SMBHANDLE
* __smb_allocate_handle()
411 _CPU_ISR_Disable(level
);
412 handle
= (SMBHANDLE
*)__lwp_objmgr_allocate(&smb_handle_objects
);
416 handle
->server_name
= NULL
;
417 handle
->share_name
= NULL
;
418 handle
->sck_server
= INVALID_SOCKET
;
419 handle
->conn_valid
= false;
420 __lwp_objmgr_open(&smb_handle_objects
,&handle
->object
);
422 _CPU_ISR_Restore(level
);
426 static void __smb_free_handle(SMBHANDLE
*handle
)
428 if(handle
->user
) free(handle
->user
);
429 if(handle
->pwd
) free(handle
->pwd
);
430 if(handle
->server_name
) free(handle
->server_name
);
431 if(handle
->share_name
) free(handle
->share_name
);
435 handle
->server_name
= NULL
;
436 handle
->share_name
= NULL
;
437 handle
->sck_server
= INVALID_SOCKET
;
439 __smb_handle_free(handle
);
442 static void MakeSMBHeader(u8 command
,u8 flags
,u16 flags2
,SMBHANDLE
*handle
)
444 u8
*ptr
= handle
->message
.smb
;
445 NBTSMB
*nbt
= &handle
->message
;
446 SMBSESSION
*sess
= &handle
->session
;
448 memset(nbt
,0,sizeof(NBTSMB
));
450 setUInt(ptr
,SMB_OFFSET_PROTO
,SMB_PROTO
);
451 setUChar(ptr
,SMB_OFFSET_CMD
,command
);
452 setUChar(ptr
,SMB_OFFSET_FLAGS
,flags
);
453 setUShort(ptr
,SMB_OFFSET_FLAGS2
,flags2
);
454 setUShort(ptr
,SMB_OFFSET_TID
,sess
->TID
);
455 setUShort(ptr
,SMB_OFFSET_PID
,sess
->PID
);
456 setUShort(ptr
,SMB_OFFSET_UID
,sess
->UID
);
457 setUShort(ptr
,SMB_OFFSET_MID
,sess
->MID
);
459 ptr
[SMB_HEADER_SIZE
] = 0;
465 static void MakeTRANS2Header(u8 subcommand
,SMBHANDLE
*handle
)
467 u8
*ptr
= handle
->message
.smb
;
469 setUChar(ptr
, T2_WORD_CNT
, 15);
470 setUShort(ptr
, T2_MAXPRM_CNT
, 10);
471 setUShort(ptr
, T2_MAXBUFFER
, 16384);
472 setUChar(ptr
, T2_SSETUP_CNT
, 1);
473 setUShort(ptr
, T2_SUB_CMD
, subcommand
);
479 * blocking call with timeout
480 * will return when ALL data has been sent. Number of bytes sent is returned.
481 * OR timeout. Timeout will return -1
482 * OR network error. -ve value will be returned
484 static inline s32
smb_send(s32 s
,const void *data
,s32 size
)
487 s32 ret
, len
= size
, nextsend
;
489 t1
=ticks_to_millisecs(gettime());
494 if(nextsend
>SMB_MAX_NET_WRITE_SIZE
)
495 nextsend
=SMB_MAX_NET_WRITE_SIZE
; //optimized value
497 ret
=net_send(s
,data
,nextsend
,0);
500 t2
=ticks_to_millisecs(gettime());
501 if( (t2
- t1
) > RECV_TIMEOUT
)
503 return -1; // timeout
505 usleep(100); // allow system to perform work. Stabilizes system
510 return ret
; // an error occurred
516 if(len
==0) return size
;
517 t1
=ticks_to_millisecs(gettime());
527 * blocking call with timeout
528 * will return when ANY data has been read from socket. Number of bytes read is returned.
529 * OR timeout. Timeout will return -1
530 * OR network error. -ve value will be returned
532 static s32
smb_recv(s32 s
,void *mem
,s32 len
)
534 s32 ret
,read
,readtotal
=0;
537 t1
=ticks_to_millisecs(gettime());
541 if(read
>SMB_MAX_NET_READ_SIZE
)
542 read
=SMB_MAX_NET_READ_SIZE
; // optimized value
544 ret
=net_recv(s
,mem
+readtotal
,read
,0);
549 if(len
==0) return readtotal
;
553 if(ret
!=-EAGAIN
) return ret
;
554 t2
=ticks_to_millisecs(gettime());
555 if( (t2
- t1
) > RECV_TIMEOUT
) return -1;
562 static void clear_network(s32 s
,u8
*ptr
)
566 t1
=ticks_to_millisecs(gettime());
569 net_recv(s
,ptr
,SMB_MAX_NET_READ_SIZE
,0);
571 t2
=ticks_to_millisecs(gettime());
572 if( (t2
- t1
) > 600) return;
580 * Do very basic checking on the return SMB
581 * Read <readlen> bytes
582 * if <readlen>==0 then read a single SMB packet
583 * discard any non NBT_SESSISON_MSG packets along the way.
585 static s32
SMBCheck(u8 command
,SMBHANDLE
*handle
)
588 u8
*ptr
= handle
->message
.smb
;
589 NBTSMB
*nbt
= &handle
->message
;
593 if(handle
->sck_server
== INVALID_SOCKET
) return SMB_ERROR
;
595 memset(nbt
,0xFF,sizeof(NBTSMB
)); //NBT_SESSISON_MSG is 0x00 so fill mem with 0xFF
597 t1
=ticks_to_millisecs(gettime());
599 /*keep going till we get a NBT session message*/
601 ret
=smb_recv(handle
->sck_server
, (u8
*)nbt
, 4);
602 if(ret
!=4) goto failed
;
604 if(nbt
->msg
!=NBT_SESSISON_MSG
)
606 readlen
=(u32
)((nbt
->length_high
<<16)|nbt
->length
);
609 t1
=ticks_to_millisecs(gettime());
610 smb_recv(handle
->sck_server
, ptr
, readlen
); //clear unexpected NBT message
613 t2
=ticks_to_millisecs(gettime());
614 if( (t2
- t1
) > RECV_TIMEOUT
* 2) goto failed
;
616 } while(nbt
->msg
!=NBT_SESSISON_MSG
);
618 /* obtain required length from NBT header if readlen==0*/
619 readlen
=(u32
)((nbt
->length_high
<<16)|nbt
->length
);
621 // Get server message block
622 ret
=smb_recv(handle
->sck_server
, ptr
, readlen
);
623 if(readlen
!=ret
) goto failed
;
625 /*** Do basic SMB Header checks ***/
626 ret
= getUInt(ptr
,SMB_OFFSET_PROTO
);
627 if(ret
!=SMB_PROTO
) goto failed
;
629 ret
= getUChar(ptr
, SMB_OFFSET_CMD
);
630 if(ret
!=command
) goto failed
;
632 ret
= getUInt(ptr
,SMB_OFFSET_NTSTATUS
);
637 clear_network(handle
->sck_server
,ptr
);
644 * Setup the SMB session, including authentication with the
645 * magic 'NTLM Response'
647 static s32
SMB_SetupAndX(SMBHANDLE
*handle
)
652 u8
*ptr
= handle
->message
.smb
;
653 SMBSESSION
*sess
= &handle
->session
;
654 char pwd
[30], ntRespData
[24];
656 if(handle
->sck_server
== INVALID_SOCKET
) return SMB_ERROR
;
658 MakeSMBHeader(SMB_SETUP_ANDX
,CIFS_FLAGS1
,handle
->unicode
?CIFS_FLAGS2_UNICODE
:CIFS_FLAGS2
,handle
);
659 pos
= SMB_HEADER_SIZE
;
661 setUChar(ptr
,pos
,13);
662 pos
++; /*** Word Count ***/
663 setUChar(ptr
,pos
,0xff);
664 pos
++; /*** Next AndX ***/
666 pos
++; /*** Reserved ***/
667 pos
+= 2; /*** Next AndX Offset ***/
668 setUShort(ptr
,pos
,sess
->MaxBuffer
);
670 setUShort(ptr
,pos
,sess
->MaxMpx
);
672 setUShort(ptr
,pos
,sess
->MaxVCS
);
674 setUInt(ptr
,pos
,sess
->sKey
);
676 setUShort(ptr
,pos
,24); /*** Password length (case-insensitive) ***/
678 setUShort(ptr
,pos
,24); /*** Password length (case-sensitive) ***/
681 pos
+= 4; /*** Reserved ***/
682 setUInt(ptr
,pos
,sess
->capabilities
);
683 pos
+= 4; /*** Capabilities ***/
685 pos
+= 2; /*** Byte count ***/
687 /*** The magic 'NTLM Response' ***/
688 strcpy(pwd
, handle
->pwd
);
689 if (sess
->challengeUsed
)
690 ntlm_smb_nt_encrypt((const char *) pwd
, (const u8
*) sess
->challenge
, (u8
*) ntRespData
);
692 /*** Build information ***/
693 memset(&ptr
[pos
],0,24);
695 memcpy(&ptr
[pos
],ntRespData
,24);
699 strcpy(pwd
, handle
->user
);
700 for(i
=0;i
<strlen(pwd
);i
++)
701 pwd
[i
] = toupper((int)pwd
[i
]);
704 pos
+= utf8_to_utf16((char*)&ptr
[pos
],pwd
,SMB_MAXPATH
-2);
709 memcpy(&ptr
[pos
],pwd
,strlen(pwd
));
710 pos
+= strlen(pwd
)+1;
713 /*** Primary Domain ***/
714 if(handle
->user
[0]=='\0') sess
->p_domain
[0] = '\0';
717 pos
+= utf8_to_utf16((char*)&ptr
[pos
],(char*)sess
->p_domain
,SMB_MAXPATH
-2);
722 memcpy(&ptr
[pos
],sess
->p_domain
,strlen((const char*)sess
->p_domain
));
723 pos
+= strlen((const char*)sess
->p_domain
)+1;
727 strcpy(pwd
,"Unix (libOGC)");
730 pos
+= utf8_to_utf16((char*)&ptr
[pos
],pwd
,SMB_MAXPATH
-2);
735 memcpy(&ptr
[pos
],pwd
,strlen(pwd
));
736 pos
+= strlen(pwd
)+1;
739 /*** Native LAN Manager ***/
740 strcpy(pwd
,"Nintendo Wii");
743 pos
+= utf8_to_utf16((char*)&ptr
[pos
],pwd
,SMB_MAXPATH
-2);
748 memcpy(&ptr
[pos
],pwd
,strlen(pwd
));
749 pos
+= strlen (pwd
)+1;
752 /*** Update byte count ***/
753 setUShort(ptr
,bcpos
,((pos
-bcpos
)-2));
755 handle
->message
.msg
= NBT_SESSISON_MSG
;
756 handle
->message
.length
= htons (pos
);
759 ret
= smb_send(handle
->sck_server
,(char*)&handle
->message
,pos
);
760 if(ret
<=0) return SMB_ERROR
;
762 if((ret
=SMBCheck(SMB_SETUP_ANDX
,handle
))==SMB_SUCCESS
) {
763 /*** Collect UID ***/
764 sess
->UID
= getUShort(handle
->message
.smb
,SMB_OFFSET_UID
);
773 * Finally, net_connect to the remote share
775 static s32
SMB_TreeAndX(SMBHANDLE
*handle
)
779 u8
*ptr
= handle
->message
.smb
;
780 SMBSESSION
*sess
= &handle
->session
;
782 if(handle
->sck_server
== INVALID_SOCKET
) return SMB_ERROR
;
784 MakeSMBHeader(SMB_TREEC_ANDX
,CIFS_FLAGS1
,handle
->unicode
?CIFS_FLAGS2_UNICODE
:CIFS_FLAGS2
,handle
);
785 pos
= SMB_HEADER_SIZE
;
788 pos
++; /*** Word Count ***/
789 setUChar(ptr
,pos
,0xff);
790 pos
++; /*** Next AndX ***/
791 pos
++; /*** Reserved ***/
792 pos
+= 2; /*** Next AndX Offset ***/
793 pos
+= 2; /*** Flags ***/
794 setUShort(ptr
,pos
,1);
795 pos
+= 2; /*** Password Length ***/
798 pos
++; /*** NULL Password ***/
800 /*** Build server share path ***/
801 strcpy ((char*)path
, "\\\\");
802 strcat ((char*)path
, handle
->server_name
);
803 strcat ((char*)path
, "\\");
804 strcat ((char*)path
, handle
->share_name
);
806 for(ret
=0;ret
<strlen((const char*)path
);ret
++)
807 path
[ret
] = (char)toupper((int)path
[ret
]);
811 pos
+= utf8_to_utf16((char*)&ptr
[pos
],path
,SMB_MAXPATH
-2);
816 memcpy(&ptr
[pos
],path
,strlen((const char*)path
));
817 pos
+= strlen((const char*)path
)+1;
821 strcpy((char*)path
,"?????");
822 memcpy(&ptr
[pos
],path
,strlen((const char*)path
));
823 pos
+= strlen((const char*)path
)+1;
826 /*** Update byte count ***/
827 setUShort(ptr
,bcpos
,(pos
-bcpos
)-2);
829 handle
->message
.msg
= NBT_SESSISON_MSG
;
830 handle
->message
.length
= htons (pos
);
833 ret
= smb_send(handle
->sck_server
,(char *)&handle
->message
,pos
);
834 if(ret
<=0) return SMB_ERROR
;
836 if((ret
=SMBCheck(SMB_TREEC_ANDX
,handle
))==SMB_SUCCESS
) {
837 /*** Collect Tree ID ***/
838 sess
->TID
= getUShort(handle
->message
.smb
,SMB_OFFSET_TID
);
845 * SMB_NegotiateProtocol
847 * The only protocol we admit to is 'NT LM 0.12'
849 static s32
SMB_NegotiateProtocol(const char *dialects
[],int dialectc
,SMBHANDLE
*handle
)
858 if(!handle
|| !dialects
|| dialectc
<=0)
861 if(handle
->sck_server
== INVALID_SOCKET
) return SMB_ERROR
;
863 /*** Clear session variables ***/
864 sess
= &handle
->session
;
865 memset(sess
,0,sizeof(SMBSESSION
));
868 sess
->capabilities
= 0;
870 MakeSMBHeader(SMB_NEG_PROTOCOL
,CIFS_FLAGS1
,handle
->unicode
?CIFS_FLAGS2_UNICODE
:CIFS_FLAGS2
,handle
);
872 pos
= SMB_HEADER_SIZE
+3;
873 ptr
= handle
->message
.smb
;
874 for(i
=0,bcnt
=0;i
<dialectc
;i
++) {
875 len
= strlen(dialects
[i
])+1;
877 memcpy(&ptr
[pos
],dialects
[i
],len
);
881 /*** Update byte count ***/
882 setUShort(ptr
,(SMB_HEADER_SIZE
+1),bcnt
);
884 /*** Set NBT information ***/
885 handle
->message
.msg
= NBT_SESSISON_MSG
;
886 handle
->message
.length
= htons(pos
);
889 ret
= smb_send(handle
->sck_server
,(char*)&handle
->message
,pos
);
890 if(ret
<=0) return SMB_ERROR
;
892 /*** Check response ***/
893 if((ret
=SMBCheck(SMB_NEG_PROTOCOL
,handle
))==SMB_SUCCESS
)
895 pos
= SMB_HEADER_SIZE
;
896 ptr
= handle
->message
.smb
;
898 /*** Collect information ***/
899 if(getUChar(ptr
,pos
)!=17) return SMB_PROTO_FAIL
; // UCHAR WordCount; Count of parameter words = 17
902 if(getUShort(ptr
,pos
)!=0) return SMB_PROTO_FAIL
; // USHORT DialectIndex; Index of selected dialect - should always be 0 since we only supplied 1!
905 if(getUChar(ptr
,pos
) & 1)
907 // user level security
908 sess
->securityLevel
= 1;
912 // share level security - can we skip SetupAndX? If so, we would need to specify the password in TreeAndX
913 sess
->securityLevel
= 0;
917 sess
->MaxMpx
= getUShort(ptr
, pos
); //USHORT MaxMpxCount; Max pending outstanding requests
920 sess
->MaxVCS
= getUShort(ptr
, pos
); //USHORT MaxNumberVcs; Max VCs between client and server
923 serverMaxBuffer
= getUInt(ptr
, pos
); //ULONG MaxBufferSize; Max transmit buffer size
926 if(serverMaxBuffer
>SMB_MAX_TRANSMIT_SIZE
)
927 sess
->MaxBuffer
= SMB_MAX_TRANSMIT_SIZE
;
929 sess
->MaxBuffer
= serverMaxBuffer
;
931 pos
+= 4; //ULONG MaxRawSize; Maximum raw buffer size
932 sess
->sKey
= getUInt(ptr
,pos
); pos
+= 4;
933 u32 servcap
= getUInt(ptr
,pos
); pos
+= 4; //ULONG Capabilities; Server capabilities
934 pos
+= 4; //ULONG SystemTimeLow; System (UTC) time of the server (low).
935 pos
+= 4; //ULONG SystemTimeHigh; System (UTC) time of the server (high).
936 sess
->timeOffset
= getShort(ptr
,pos
) * 600000000LL; pos
+= 2; //SHORT ServerTimeZone; Time zone of server (minutes from UTC)
938 //UCHAR EncryptionKeyLength - 0 or 8
939 if(getUChar(ptr
,pos
)!=8)
941 if (getUChar(ptr
,pos
)!=0)
943 return SMB_BAD_KEYLEN
;
947 // Challenge key not used
948 sess
->challengeUsed
= false;
953 sess
->challengeUsed
= true;
957 getUShort(ptr
,pos
); // byte count
959 if (sess
->challengeUsed
)
961 /*** Copy challenge key ***/
963 memcpy(&sess
->challenge
,&ptr
[pos
],8);
966 /*** Primary domain ***/
969 while(ptr
[pos
+j
]!=0) {
970 sess
->p_domain
[i
] = ptr
[pos
+j
];
974 sess
->p_domain
[i
] = '\0';
976 // setup capabilities
977 //if(servcap & CAP_LARGE_FILES)
978 // sess->capabilities |= CAP_LARGE_FILES;
980 if(servcap
& CAP_UNICODE
)
982 sess
->capabilities
|= CAP_UNICODE
;
983 handle
->unicode
= true;
991 static s32
do_netconnect(SMBHANDLE
*handle
)
998 handle
->sck_server
= INVALID_SOCKET
;
999 /*** Create the global net_socket ***/
1000 sock
= net_socket(AF_INET
, SOCK_STREAM
, IPPROTO_IP
);
1001 if(sock
==INVALID_SOCKET
) return -1;
1003 // Switch off Nagle with TCP_NODELAY
1004 net_setsockopt(sock
,IPPROTO_TCP
,TCP_NODELAY
,&set
,sizeof(set
));
1006 // create non blocking socket
1007 ret
= net_ioctl(sock
, FIONBIO
, &set
);
1014 t1
=ticks_to_millisecs(gettime());
1017 ret
= net_connect(sock
,(struct sockaddr
*)&handle
->server_addr
,sizeof(handle
->server_addr
));
1018 if(ret
==-EISCONN
) break;
1019 t2
=ticks_to_millisecs(gettime());
1021 if((t2
-t1
) > CONN_TIMEOUT
) break; // usually not more than 90ms
1030 handle
->sck_server
= sock
;
1034 static s32
do_smbconnect(SMBHANDLE
*handle
)
1038 if(handle
->sck_server
== INVALID_SOCKET
) return -1;
1040 ret
= SMB_NegotiateProtocol(smb_dialects
,smb_dialectcnt
,handle
);
1041 if(ret
!=SMB_SUCCESS
)
1043 net_close(handle
->sck_server
);
1044 handle
->sck_server
= INVALID_SOCKET
;
1048 ret
= SMB_SetupAndX(handle
);
1049 if(ret
!=SMB_SUCCESS
)
1051 net_close(handle
->sck_server
);
1052 handle
->sck_server
= INVALID_SOCKET
;
1056 ret
= SMB_TreeAndX(handle
);
1057 if(ret
!=SMB_SUCCESS
)
1059 net_close(handle
->sck_server
);
1060 handle
->sck_server
= INVALID_SOCKET
;
1064 handle
->conn_valid
= true;
1068 /****************************************************************************
1069 * Create an NBT SESSION REQUEST message.
1070 ****************************************************************************/
1071 static int MakeSessReq(unsigned char *bufr
, unsigned char *Called
, unsigned char *Calling
)
1073 // Write the header.
1077 bufr
[3] = 68; // 2x34 bytes in length.
1079 // Copy the Called and Calling names into the buffer.
1080 (void) memcpy(&bufr
[4], Called
, 34);
1081 (void) memcpy(&bufr
[38], Calling
, 34);
1083 // Return the total message length.
1087 static unsigned char *L1_Encode(unsigned char *dst
, const unsigned char *name
,
1088 const unsigned char pad
, const unsigned char sfx
)
1094 while (('\0' != name
[i
]) && (i
< 15))
1096 k
= toupper(name
[i
++]);
1097 dst
[j
++] = 'A' + ((k
& 0xF0) >> 4);
1098 dst
[j
++] = 'A' + (k
& 0x0F);
1101 i
= 'A' + ((pad
& 0xF0) >> 4);
1102 k
= 'A' + (pad
& 0x0F);
1109 dst
[30] = 'A' + ((sfx
& 0xF0) >> 4);
1110 dst
[31] = 'A' + (sfx
& 0x0F);
1116 static int L2_Encode(unsigned char *dst
, const unsigned char *name
,
1117 const unsigned char pad
, const unsigned char sfx
,
1118 const unsigned char *scope
)
1124 if (NULL
== L1_Encode(&dst
[1], name
, pad
, sfx
))
1134 for (i
= 0, j
= (lenpos
+ 1); ('.' != scope
[i
]) && ('\0'
1135 != scope
[i
]); i
++, j
++)
1136 dst
[j
] = toupper(scope
[i
]);
1138 dst
[lenpos
] = (unsigned char) i
;
1141 } while ('.' == *(scope
++));
1146 return (lenpos
+ 1);
1149 /****************************************************************************
1150 * Send an NBT SESSION REQUEST over the TCP connection, then wait for a reply.
1151 ****************************************************************************/
1152 static s32
SMB_RequestNBTSession(SMBHANDLE
*handle
)
1154 unsigned char Called
[34];
1155 unsigned char Calling
[34];
1156 unsigned char bufr
[128];
1159 if(handle
->sck_server
== INVALID_SOCKET
) return -1;
1161 L2_Encode(Called
, (const unsigned char*) "*SMBSERVER", 0x20, 0x20,
1162 (const unsigned char*) "");
1163 L2_Encode(Calling
, (const unsigned char*) "SMBCLIENT", 0x20, 0x00,
1164 (const unsigned char*) "");
1166 // Create the NBT Session Request message.
1167 result
= MakeSessReq(bufr
, Called
, Calling
);
1169 //Send the NBT Session Request message.
1170 result
= smb_send(handle
->sck_server
, bufr
, result
);
1173 // Error sending Session Request message
1177 // Now wait for and handle the reply (2 seconds).
1178 result
= smb_recv(handle
->sck_server
, bufr
, 128);
1181 // Timeout waiting for NBT Session Response
1188 // Positive Session Response
1192 // Negative Session Response
1196 // Retarget Session Response
1200 // Unexpected Session Response
1205 /****************************************************************************
1206 * Primary setup, logon and connection all in one :)
1207 ****************************************************************************/
1208 s32
SMB_Connect(SMBCONN
*smbhndl
, const char *user
, const char *password
, const char *share
, const char *server
)
1214 *smbhndl
= SMB_HANDLE_NULL
;
1216 if(!user
|| !password
|| !share
|| !server
||
1217 strlen(user
) > 20 || strlen(password
) > 14 ||
1218 strlen(share
) > 80 || strlen(server
) > 80)
1220 return SMB_BAD_LOGINDATA
;
1226 _CPU_ISR_Disable(level
);
1228 _CPU_ISR_Restore(level
);
1231 handle
= __smb_allocate_handle();
1232 if(!handle
) return SMB_ERROR
;
1234 handle
->user
= strdup(user
);
1235 handle
->pwd
= strdup(password
);
1236 handle
->server_name
= strdup(server
);
1237 handle
->share_name
= strdup(share
);
1238 handle
->server_addr
.sin_family
= AF_INET
;
1239 handle
->server_addr
.sin_port
= htons(445);
1240 handle
->unicode
= false;
1242 if(strlen(server
) < 16 && inet_aton(server
, &val
))
1244 handle
->server_addr
.sin_addr
.s_addr
= val
.s_addr
;
1246 else // might be a hostname
1249 struct hostent
*hp
= net_gethostbyname(server
);
1250 if (!hp
|| !(hp
->h_addrtype
== PF_INET
))
1251 ret
= SMB_BAD_LOGINDATA
;
1253 memcpy((char *)&handle
->server_addr
.sin_addr
.s_addr
, hp
->h_addr_list
[0], hp
->h_length
);
1255 __smb_free_handle(handle
);
1260 *smbhndl
=(SMBCONN
)(LWP_OBJMASKTYPE(SMB_OBJTYPE_HANDLE
)|LWP_OBJMASKID(handle
->object
.id
));
1264 ret
= do_netconnect(handle
);
1265 if(ret
==0) ret
= do_smbconnect(handle
);
1270 handle
->server_addr
.sin_port
= htons(139);
1271 ret
= do_netconnect(handle
);
1272 if(ret
==0) ret
= SMB_RequestNBTSession(handle
);
1273 if(ret
==0) ret
= do_smbconnect(handle
);
1278 __smb_free_handle(handle
);
1285 /****************************************************************************
1287 ****************************************************************************/
1288 void SMB_Close(SMBCONN smbhndl
)
1290 SMBHANDLE
*handle
= __smb_handle_open(smbhndl
);
1293 if(handle
->sck_server
!=INVALID_SOCKET
)
1294 net_close(handle
->sck_server
);
1296 __smb_free_handle(handle
);
1299 s32
SMB_Reconnect(SMBCONN
*_smbhndl
, bool test_conn
)
1301 s32 ret
= SMB_SUCCESS
;
1302 SMBCONN smbhndl
= *_smbhndl
;
1303 SMBHANDLE
*handle
= __smb_handle_open(smbhndl
);
1305 return SMB_ERROR
; // we have no handle, so we can't reconnect
1307 if(handle
->conn_valid
&& test_conn
)
1310 if(SMB_PathInfo("\\", &dentry
, smbhndl
)==SMB_SUCCESS
) return SMB_SUCCESS
; // no need to reconnect
1311 handle
->conn_valid
= false; // else connection is invalid
1313 if(!handle
->conn_valid
)
1315 // shut down connection
1316 if(handle
->sck_server
!=INVALID_SOCKET
)
1318 net_close(handle
->sck_server
);
1319 handle
->sck_server
= INVALID_SOCKET
;
1323 if(handle
->server_addr
.sin_port
> 0)
1325 ret
= do_netconnect(handle
);
1326 if(ret
==0 && handle
->server_addr
.sin_port
== htons(139))
1327 ret
= SMB_RequestNBTSession(handle
);
1329 ret
= do_smbconnect(handle
);
1331 else // initial connection
1333 handle
->server_addr
.sin_port
= htons(445);
1334 ret
= do_netconnect(handle
);
1335 if(ret
==0) ret
= do_smbconnect(handle
);
1340 handle
->server_addr
.sin_port
= htons(139);
1341 ret
= do_netconnect(handle
);
1342 if(ret
==0) ret
= SMB_RequestNBTSession(handle
);
1343 if(ret
==0) ret
= do_smbconnect(handle
);
1347 handle
->server_addr
.sin_port
= 0;
1353 SMBFILE
SMB_OpenFile(const char *filename
, u16 access
, u16 creation
,SMBCONN smbhndl
)
1358 struct _smbfile
*fid
= NULL
;
1362 if(filename
== NULL
)
1365 if(SMB_Reconnect(&smbhndl
,true)!=SMB_SUCCESS
) return NULL
;
1367 handle
= __smb_handle_open(smbhndl
);
1368 if(!handle
) return NULL
;
1370 MakeSMBHeader(SMB_OPEN_ANDX
,CIFS_FLAGS1
,handle
->unicode
?CIFS_FLAGS2_UNICODE
:CIFS_FLAGS2
,handle
);
1372 pos
= SMB_HEADER_SIZE
;
1373 ptr
= handle
->message
.smb
;
1374 setUChar(ptr
, pos
, 15);
1375 pos
++; /*** Word Count ***/
1376 setUChar(ptr
, pos
, 0xff);
1377 pos
++; /*** AndXCommand 0xFF = None ***/
1378 setUChar(ptr
, pos
, 0);
1379 pos
++; /*** AndX Reserved must be 0 ***/
1380 pos
+= 2; /*** Next AndX Offset to next Command ***/
1381 pos
+= 2; /*** Flags ***/
1382 setUShort(ptr
, pos
, access
);
1383 pos
+= 2; /*** Access mode ***/
1384 setUShort(ptr
, pos
, 0x6);
1385 pos
+= 2; /*** Type of file ***/
1386 pos
+= 2; /*** File Attributes ***/
1387 pos
+= 4; /*** File time - don't care - let server decide ***/
1388 setUShort(ptr
, pos
, creation
);
1389 pos
+= 2; /*** Creation flags ***/
1390 pos
+= 4; /*** Allocation size ***/
1391 setUInt(ptr
, pos
, 0);
1392 pos
+= 4; /*** Reserved[0] must be 0 ***/
1393 setUInt(ptr
, pos
, 0);
1394 pos
+= 4; /*** Reserved[1] must be 0 ***/
1395 pos
+= 2; /*** Byte Count ***/
1397 setUChar(ptr
, pos
, 0x04); /** Bufferformat **/
1401 if (filename
[0] != '\\')
1402 strcpy(realfile
,"\\");
1403 strcat(realfile
,filename
);
1407 pos
+= utf8_to_utf16((char*)&ptr
[pos
],realfile
,SMB_MAXPATH
-2);
1412 memcpy(&ptr
[pos
],realfile
,strlen(realfile
));
1413 pos
+= strlen(realfile
)+1;
1416 setUShort(ptr
,(bpos
-2),(pos
-bpos
));
1418 handle
->message
.msg
= NBT_SESSISON_MSG
;
1419 handle
->message
.length
= htons(pos
);
1422 ret
= smb_send(handle
->sck_server
,(char*)&handle
->message
,pos
);
1423 if(ret
<0) goto failed
;
1425 if(SMBCheck(SMB_OPEN_ANDX
,handle
)==SMB_SUCCESS
) {
1426 /*** Check file handle ***/
1427 fid
= (struct _smbfile
*)__lwp_queue_get(&smb_filehandle_queue
);
1429 fid
->conn
= smbhndl
;
1430 fid
->sfid
= getUShort(handle
->message
.smb
,(SMB_HEADER_SIZE
+5));
1433 return (SMBFILE
)fid
;
1436 handle
->conn_valid
= false;
1443 void SMB_CloseFile(SMBFILE sfid
)
1448 struct _smbfile
*fid
= (struct _smbfile
*)sfid
;
1452 handle
= __smb_handle_open(fid
->conn
);
1455 MakeSMBHeader(SMB_CLOSE
,CIFS_FLAGS1
,handle
->unicode
?CIFS_FLAGS2_UNICODE
:CIFS_FLAGS2
,handle
);
1457 pos
= SMB_HEADER_SIZE
;
1458 ptr
= handle
->message
.smb
;
1459 setUChar(ptr
, pos
, 3);
1460 pos
++; /** Word Count **/
1461 setUShort(ptr
, pos
, fid
->sfid
);
1463 setUInt(ptr
, pos
, 0xffffffff);
1464 pos
+= 4; /*** Last Write ***/
1465 pos
+= 2; /*** Byte Count ***/
1467 handle
->message
.msg
= NBT_SESSISON_MSG
;
1468 handle
->message
.length
= htons(pos
);
1471 ret
= smb_send(handle
->sck_server
,(char*)&handle
->message
,pos
);
1472 if(ret
<0) handle
->conn_valid
= false;
1473 else SMBCheck(SMB_CLOSE
,handle
);
1474 __lwp_queue_append(&smb_filehandle_queue
,&fid
->node
);
1478 * SMB_CreateDirectory
1480 s32
SMB_CreateDirectory(const char *dirname
, SMBCONN smbhndl
)
1491 if(SMB_Reconnect(&smbhndl
,true)!=SMB_SUCCESS
) return -1;
1493 handle
= __smb_handle_open(smbhndl
);
1494 if(!handle
) return -1;
1496 MakeSMBHeader(SMB_COM_CREATE_DIRECTORY
,CIFS_FLAGS1
, handle
->unicode
?CIFS_FLAGS2_UNICODE
:CIFS_FLAGS2
,handle
);
1498 pos
= SMB_HEADER_SIZE
;
1499 ptr
= handle
->message
.smb
;
1500 setUChar(ptr
, pos
, 0);
1501 pos
++; /*** Word Count ***/
1502 pos
+= 2; /*** Byte Count ***/
1504 setUChar(ptr
, pos
, 0x04); /*** Buffer format ***/
1508 if (dirname
[0] != '\\')
1509 strcpy(realfile
,"\\");
1510 strcat(realfile
,dirname
);
1514 pos
+= utf8_to_utf16((char*)&ptr
[pos
],realfile
,SMB_MAXPATH
-2);
1519 memcpy(&ptr
[pos
],realfile
,strlen(realfile
));
1520 pos
+= strlen(realfile
)+1;
1523 setUShort(ptr
,(bpos
-2),(pos
-bpos
));
1525 handle
->message
.msg
= NBT_SESSISON_MSG
;
1526 handle
->message
.length
= htons(pos
);
1529 ret
= smb_send(handle
->sck_server
,(char*)&handle
->message
,pos
);
1530 if(ret
< 0) goto failed
;
1532 ret
= SMBCheck(SMB_COM_CREATE_DIRECTORY
,handle
);
1541 * SMB_DeleteDirectory
1543 s32
SMB_DeleteDirectory(const char *dirname
, SMBCONN smbhndl
)
1554 if(SMB_Reconnect(&smbhndl
,true)!=SMB_SUCCESS
) return -1;
1556 handle
= __smb_handle_open(smbhndl
);
1557 if(!handle
) return -1;
1559 MakeSMBHeader(SMB_COM_DELETE_DIRECTORY
,CIFS_FLAGS1
, handle
->unicode
?CIFS_FLAGS2_UNICODE
:CIFS_FLAGS2
,handle
);
1561 pos
= SMB_HEADER_SIZE
;
1562 ptr
= handle
->message
.smb
;
1563 setUChar(ptr
, pos
, 0);
1564 pos
++; /*** Word Count ***/
1565 pos
+= 2; /*** Byte Count ***/
1567 setUChar(ptr
, pos
, 0x04); /** Bufferformat **/
1571 if (dirname
[0] != '\\')
1572 strcpy(realfile
,"\\");
1573 strcat(realfile
,dirname
);
1577 pos
+= utf8_to_utf16((char*)&ptr
[pos
],realfile
,SMB_MAXPATH
-2);
1582 memcpy(&ptr
[pos
],realfile
,strlen(realfile
));
1583 pos
+= strlen(realfile
)+1;
1586 setUShort(ptr
,(bpos
-2),(pos
-bpos
));
1588 handle
->message
.msg
= NBT_SESSISON_MSG
;
1589 handle
->message
.length
= htons(pos
);
1592 ret
= smb_send(handle
->sck_server
,(char*)&handle
->message
,pos
);
1593 if(ret
< 0) goto failed
;
1595 ret
= SMBCheck(SMB_COM_DELETE_DIRECTORY
,handle
);
1606 s32
SMB_DeleteFile(const char *filename
, SMBCONN smbhndl
)
1614 if(filename
== NULL
)
1617 if(SMB_Reconnect(&smbhndl
,true)!=SMB_SUCCESS
) return -1;
1619 handle
= __smb_handle_open(smbhndl
);
1620 if(!handle
) return -1;
1622 MakeSMBHeader(SMB_COM_DELETE
,CIFS_FLAGS1
, handle
->unicode
?CIFS_FLAGS2_UNICODE
:CIFS_FLAGS2
,handle
);
1624 pos
= SMB_HEADER_SIZE
;
1625 ptr
= handle
->message
.smb
;
1626 setUChar(ptr
, pos
, 1);
1627 pos
++; /*** Word Count ***/
1628 setUShort(ptr
, pos
, SMB_SRCH_HIDDEN
);
1629 pos
+= 2; /*** SearchAttributes ***/
1630 pos
+= 2; /*** Byte Count ***/
1632 setUChar(ptr
, pos
, 0x04); /** Bufferformat **/
1636 if (filename
[0] != '\\')
1637 strcpy(realfile
,"\\");
1638 strcat(realfile
,filename
);
1642 pos
+= utf8_to_utf16((char*)&ptr
[pos
],realfile
,SMB_MAXPATH
-2);
1647 memcpy(&ptr
[pos
],realfile
,strlen(realfile
));
1648 pos
+= strlen(realfile
)+1;
1651 setUShort(ptr
,(bpos
-2),(pos
-bpos
));
1653 handle
->message
.msg
= NBT_SESSISON_MSG
;
1654 handle
->message
.length
= htons(pos
);
1657 ret
= smb_send(handle
->sck_server
,(char*)&handle
->message
,pos
);
1658 if(ret
< 0) goto failed
;
1660 ret
= SMBCheck(SMB_COM_DELETE
,handle
);
1671 s32
SMB_Rename(const char *filename
, const char * newfilename
, SMBCONN smbhndl
)
1678 char newrealfile
[512];
1680 if(filename
== NULL
|| newfilename
== NULL
)
1683 if(SMB_Reconnect(&smbhndl
,true)!=SMB_SUCCESS
)
1686 handle
= __smb_handle_open(smbhndl
);
1687 if(!handle
) return -1;
1689 MakeSMBHeader(SMB_COM_RENAME
,CIFS_FLAGS1
, handle
->unicode
?CIFS_FLAGS2_UNICODE
:CIFS_FLAGS2
,handle
);
1691 pos
= SMB_HEADER_SIZE
;
1692 ptr
= handle
->message
.smb
;
1693 setUChar(ptr
, pos
, 1);
1694 pos
++; /*** Word Count ***/
1695 setUShort(ptr
, pos
, SMB_SRCH_HIDDEN
);
1696 pos
+= 2; /*** SearchAttributes ***/
1697 pos
+= 2; /*** Byte Count ***/
1699 setUChar(ptr
, pos
, 0x04); /** Bufferformat **/
1703 if (filename
[0] != '\\')
1704 strcpy(realfile
,"\\");
1705 strcat(realfile
,filename
);
1709 pos
+= utf8_to_utf16((char*)&ptr
[pos
],realfile
,SMB_MAXPATH
-2);
1714 memcpy(&ptr
[pos
],realfile
,strlen(realfile
));
1715 pos
+= strlen(realfile
)+1;
1719 setUChar(ptr
, pos
, 0x04); /** Bufferformat **/
1722 newrealfile
[0]='\0';
1723 if (newfilename
[0] != '\\')
1724 strcpy(newrealfile
,"\\");
1725 strcat(newrealfile
,newfilename
);
1729 pos
+= utf8_to_utf16((char*)&ptr
[pos
],newrealfile
,SMB_MAXPATH
-2);
1734 memcpy(&ptr
[pos
],newrealfile
,strlen(newrealfile
));
1735 pos
+= strlen(newrealfile
)+1;
1738 setUShort(ptr
,(bpos
-2),(pos
-bpos
));
1740 handle
->message
.msg
= NBT_SESSISON_MSG
;
1741 handle
->message
.length
= htons(pos
);
1744 ret
= smb_send(handle
->sck_server
,(char*)&handle
->message
,pos
);
1745 if(ret
< 0) goto failed
;
1747 ret
= SMBCheck(SMB_COM_RENAME
,handle
);
1756 * SMB_DiskInformation
1758 s32
SMB_DiskInformation(struct statvfs
*buf
, SMBCONN smbhndl
)
1762 u16 TotalUnits
, BlocksPerUnit
, BlockSize
, FreeUnits
;
1766 if(SMB_Reconnect(&smbhndl
,true)!=SMB_SUCCESS
) return -1;
1768 handle
= __smb_handle_open(smbhndl
);
1769 if(!handle
) return -1;
1771 MakeSMBHeader(SMB_COM_QUERY_INFORMATION_DISK
,CIFS_FLAGS1
, handle
->unicode
?CIFS_FLAGS2_UNICODE
:CIFS_FLAGS2
,handle
);
1773 pos
= SMB_HEADER_SIZE
;
1774 ptr
= handle
->message
.smb
;
1775 setUChar(ptr
, pos
, 0);
1776 pos
++; /*** Word Count ***/
1777 setUShort(ptr
, pos
, 0);
1778 pos
+= 2; /*** Byte Count ***/
1780 handle
->message
.msg
= NBT_SESSISON_MSG
;
1781 handle
->message
.length
= htons(pos
);
1784 ret
= smb_send(handle
->sck_server
,(char*)&handle
->message
,pos
);
1785 if(ret
< 0) goto failed
;
1787 if((ret
=SMBCheck(SMB_COM_QUERY_INFORMATION_DISK
, handle
))==SMB_SUCCESS
)
1789 ptr
= handle
->message
.smb
;
1790 /** Read the received data ***/
1794 TotalUnits
= getUShort(ptr
,(SMB_HEADER_SIZE
+recv_pos
));
1796 /** BlocksPerUnit **/
1797 BlocksPerUnit
= getUShort(ptr
,(SMB_HEADER_SIZE
+recv_pos
));
1800 BlockSize
= getUShort(ptr
,(SMB_HEADER_SIZE
+recv_pos
));
1803 FreeUnits
= getUShort(ptr
,(SMB_HEADER_SIZE
+recv_pos
));
1806 buf
->f_bsize
= (unsigned long) BlockSize
; // File system block size.
1807 buf
->f_frsize
= (unsigned long) BlockSize
; // Fundamental file system block size.
1808 buf
->f_blocks
= (fsblkcnt_t
) (TotalUnits
*BlocksPerUnit
); // Total number of blocks on file system in units of f_frsize.
1809 buf
->f_bfree
= (fsblkcnt_t
) (FreeUnits
*BlocksPerUnit
); // Total number of free blocks.
1810 buf
->f_bavail
= 0; // Number of free blocks available to non-privileged process.
1811 buf
->f_files
= 0; // Total number of file serial numbers.
1812 buf
->f_ffree
= 0; // Total number of free file serial numbers.
1813 buf
->f_favail
= 0; // Number of file serial numbers available to non-privileged process.
1814 buf
->f_fsid
= 0; // File system ID. 32bit ioType value
1815 buf
->f_flag
= 0; // Bit mask of f_flag values.
1816 buf
->f_namemax
= SMB_MAXPATH
; // Maximum filename length.
1822 handle
->conn_valid
= false;
1829 s32
SMB_ReadFile(char *buffer
, size_t size
, off_t offset
, SMBFILE sfid
)
1835 size_t totalread
=0,nextread
;
1836 struct _smbfile
*fid
= (struct _smbfile
*)sfid
;
1840 // Check for invalid size
1841 if(size
== 0) return -1;
1843 handle
= __smb_handle_open(fid
->conn
);
1844 if(!handle
) return -1;
1846 while(totalread
< size
)
1848 if((size
-totalread
) > SMB_MAX_TRANSMIT_SIZE
)
1849 nextread
=SMB_MAX_TRANSMIT_SIZE
;
1851 nextread
=size
-totalread
;
1853 MakeSMBHeader(SMB_READ_ANDX
,CIFS_FLAGS1
,handle
->unicode
?CIFS_FLAGS2_UNICODE
:CIFS_FLAGS2
,handle
);
1855 pos
= SMB_HEADER_SIZE
;
1856 ptr
= handle
->message
.smb
;
1857 setUChar(ptr
, pos
, 12);
1858 pos
++; /*** Word count ***/
1859 setUChar(ptr
, pos
, 0xff);
1861 setUChar(ptr
, pos
, 0);
1862 pos
++; /*** Reserved must be 0 ***/
1863 pos
+= 2; /*** Next AndX Offset ***/
1864 setUShort(ptr
, pos
, fid
->sfid
);
1865 pos
+= 2; /*** FID ***/
1866 setUInt(ptr
, pos
, (offset
+totalread
) & 0xffffffff);
1867 pos
+= 4; /*** Offset ***/
1869 setUShort(ptr
, pos
, nextread
& 0xffff);
1871 setUShort(ptr
, pos
, nextread
& 0xffff);
1873 setUInt(ptr
, pos
, 0);
1874 pos
+= 4; /*** Reserved must be 0 ***/
1875 setUShort(ptr
, pos
, nextread
& 0xffff);
1876 pos
+= 2; /*** Remaining ***/
1877 setUInt(ptr
, pos
, (offset
+totalread
) >> 32); // offset high
1878 pos
+= 4; /*** OffsetHIGH ***/
1879 pos
+= 2; /*** Byte count ***/
1881 handle
->message
.msg
= NBT_SESSISON_MSG
;
1882 handle
->message
.length
= htons(pos
);
1886 ret
= smb_send(handle
->sck_server
,(char*)&handle
->message
, pos
);
1887 if(ret
<0) goto failed
;
1890 if(SMBCheck(SMB_READ_ANDX
,handle
)!=SMB_SUCCESS
) goto failed
;
1892 ptr
= handle
->message
.smb
;
1893 // Retrieve data length for this packet
1894 length
= getUShort(ptr
,(SMB_HEADER_SIZE
+11));
1899 // Retrieve offset to data
1900 ofs
= getUShort(ptr
,(SMB_HEADER_SIZE
+13));
1901 memcpy(&buffer
[totalread
],&ptr
[ofs
],length
);
1907 handle
->conn_valid
= false;
1914 s32
SMB_WriteFile(const char *buffer
, size_t size
, off_t offset
, SMBFILE sfid
)
1921 struct _smbfile
*fid
= (struct _smbfile
*)sfid
;
1925 handle
= __smb_handle_open(fid
->conn
);
1926 if(!handle
) return -1;
1928 MakeSMBHeader(SMB_WRITE_ANDX
,CIFS_FLAGS1
,handle
->unicode
?CIFS_FLAGS2_UNICODE
:CIFS_FLAGS2
,handle
);
1931 pos
= SMB_HEADER_SIZE
;
1932 ptr
= handle
->message
.smb
;
1933 setUChar(ptr
, pos
, 14);
1934 pos
++; /*** Word Count ***/
1935 setUChar(ptr
, pos
, 0xff);
1936 pos
+= 2; /*** Next AndX ***/
1937 pos
+= 2; /*** Next AndX Offset ***/
1939 setUShort(ptr
, pos
, fid
->sfid
);
1941 setUInt(ptr
, pos
, offset
& 0xffffffff);
1943 setUInt(ptr
, pos
, 0); /*** Reserved, must be 0 ***/
1945 setUShort(ptr
, pos
, 0); /*** Write Mode ***/
1947 pos
+= 2; /*** Remaining ***/
1949 blocks64
= size
>> 16;
1951 setUShort(ptr
, pos
, blocks64
);
1952 pos
+= 2; /*** Length High ***/
1953 setUShort(ptr
, pos
, size
& 0xffff);
1954 pos
+= 2; /*** Length Low ***/
1955 setUShort(ptr
, pos
, 63);
1956 pos
+= 2; /*** Data Offset ***/
1957 setUInt(ptr
, pos
, offset
>> 32); /*** OffsetHigh ***/
1959 setUShort(ptr
, pos
, size
& 0xffff);
1960 pos
+= 2; /*** Data Byte Count ***/
1962 handle
->message
.msg
= NBT_SESSISON_MSG
;
1963 handle
->message
.length
= htons(pos
+size
);
1968 if((copy_len
+pos
)>SMB_MAX_TRANSMIT_SIZE
)
1969 copy_len
= (SMB_MAX_TRANSMIT_SIZE
-pos
);
1971 memcpy(&ptr
[pos
],src
,copy_len
);
1978 /*** Send Header Information ***/
1979 ret
= smb_send(handle
->sck_server
,(char*)&handle
->message
,pos
);
1980 if(ret
<0) goto failed
;
1983 /*** Send the data ***/
1984 ret
= smb_send(handle
->sck_server
,src
,size
);
1985 if(ret
<0) goto failed
;
1989 if(SMBCheck(SMB_WRITE_ANDX
,handle
)==SMB_SUCCESS
) {
1990 ptr
= handle
->message
.smb
;
1991 ret
= getUShort(ptr
,(SMB_HEADER_SIZE
+5));
1997 handle
->conn_valid
= false;
2004 s32
SMB_PathInfo(const char *filename
, SMBDIRENTRY
*sdir
, SMBCONN smbhndl
)
2014 if(filename
== NULL
)
2017 handle
= __smb_handle_open(smbhndl
);
2018 if (!handle
) return SMB_ERROR
;
2020 MakeSMBHeader(SMB_TRANS2
, CIFS_FLAGS1
, handle
->unicode
?CIFS_FLAGS2_UNICODE
:CIFS_FLAGS2
, handle
);
2021 MakeTRANS2Header(SMB_QUERY_PATH_INFO
, handle
);
2023 bpos
= pos
= (T2_BYTE_CNT
+ 2);
2024 pos
+= 3; /*** Padding ***/
2025 ptr
= handle
->message
.smb
;
2026 setUShort(ptr
, pos
, SMB_QUERY_FILE_ALL_INFO
);
2027 pos
+= 2; /*** Level of information requested ***/
2028 setUInt(ptr
, pos
, 0);
2029 pos
+= 4; /*** reserved ***/
2032 if (filename
[0] != '\\')
2033 strcpy(realfile
,"\\");
2034 strcat(realfile
,filename
);
2038 len
= utf8_to_utf16((char*)&ptr
[pos
],realfile
,SMB_MAXPATH
-2);
2044 len
= strlen(realfile
);
2045 memcpy(&ptr
[pos
],realfile
,len
);
2049 /*** Update counts ***/
2050 setUShort(ptr
, T2_PRM_CNT
, (7 + len
));
2051 setUShort(ptr
, T2_SPRM_CNT
, (7 + len
));
2052 setUShort(ptr
, T2_SPRM_OFS
, 68);
2053 setUShort(ptr
, T2_BYTE_CNT
, (pos
- bpos
));
2055 handle
->message
.msg
= NBT_SESSISON_MSG
;
2056 handle
->message
.length
= htons(pos
);
2059 ret
= smb_send(handle
->sck_server
, (char*) &handle
->message
, pos
);
2060 if(ret
<0) goto failed
;
2063 if (SMBCheck(SMB_TRANS2
, handle
) == SMB_SUCCESS
) {
2065 ptr
= handle
->message
.smb
;
2066 /*** Get parameter offset ***/
2067 pos
= getUShort(ptr
, (SMB_HEADER_SIZE
+ 9));
2068 pos
+= 4; // padding
2069 sdir
->ctime
= getULongLong(ptr
, pos
) - handle
->session
.timeOffset
; pos
+= 8; // ULONGLONG - creation time
2070 sdir
->atime
= getULongLong(ptr
, pos
) - handle
->session
.timeOffset
; pos
+= 8; // ULONGLONG - access time
2071 sdir
->mtime
= getULongLong(ptr
, pos
) - handle
->session
.timeOffset
; pos
+= 8; // ULONGLONG - write time
2072 pos
+= 8; // ULONGLONG - change time
2073 sdir
->attributes
= getUInt(ptr
,pos
); pos
+= 4; // ULONG - file attributes
2074 pos
+= 4; // padding
2075 pos
+= 8; // ULONGLONG - allocated file size
2076 sdir
->size
= getULongLong(ptr
, pos
); pos
+= 8; // ULONGLONG - file size
2077 pos
+= 4; // ULONG NumberOfLinks;
2078 pos
+= 2; // UCHAR DeletePending;
2079 pos
+= 2; // UCHAR Directory;
2080 pos
+= 2; // USHORT Pad2; // for alignment only
2081 pos
+= 4; // ULONG EaSize;
2082 pos
+= 4; // ULONG FileNameLength;
2084 strcpy(sdir
->name
,realfile
);
2091 handle
->conn_valid
= false;
2098 * Uses TRANS2 to support long filenames
2100 s32
SMB_FindFirst(const char *filename
, unsigned short flags
, SMBDIRENTRY
*sdir
, SMBCONN smbhndl
)
2110 if(filename
== NULL
)
2113 if(SMB_Reconnect(&smbhndl
,true)!=SMB_SUCCESS
) return SMB_ERROR
;
2115 handle
= __smb_handle_open(smbhndl
);
2116 if(!handle
) return SMB_ERROR
;
2118 sess
= &handle
->session
;
2119 MakeSMBHeader(SMB_TRANS2
,CIFS_FLAGS1
,handle
->unicode
?CIFS_FLAGS2_UNICODE
:CIFS_FLAGS2
,handle
);
2120 MakeTRANS2Header(SMB_FIND_FIRST2
,handle
);
2122 ptr
= handle
->message
.smb
;
2124 bpos
= pos
= (T2_BYTE_CNT
+2);
2125 pos
+= 3; /*** Padding ***/
2126 setUShort(ptr
, pos
, flags
);
2127 pos
+= 2; /*** Flags ***/
2128 setUShort(ptr
, pos
, 1);
2129 pos
+= 2; /*** Count ***/
2130 setUShort(ptr
, pos
, 6);
2131 pos
+= 2; /*** Internal Flags ***/
2132 setUShort(ptr
, pos
, 260); // SMB_FIND_FILE_BOTH_DIRECTORY_INFO
2133 pos
+= 2; /*** Level of Interest ***/
2134 pos
+= 4; /*** Storage Type == 0 ***/
2138 len
= utf8_to_utf16((char*)&ptr
[pos
], (char*)filename
,SMB_MAXPATH
-2);
2144 len
= strlen(filename
);
2145 memcpy(&ptr
[pos
],filename
,len
);
2149 /*** Update counts ***/
2150 setUShort(ptr
, T2_PRM_CNT
, (13+len
));
2151 setUShort(ptr
, T2_SPRM_CNT
, (13+len
));
2152 setUShort(ptr
, T2_SPRM_OFS
, 68);
2153 setUShort(ptr
, T2_BYTE_CNT
,(pos
-bpos
));
2155 handle
->message
.msg
= NBT_SESSISON_MSG
;
2156 handle
->message
.length
= htons(pos
);
2159 ret
= smb_send(handle
->sck_server
,(char*)&handle
->message
,pos
);
2160 if(ret
<0) goto failed
;
2165 if(SMBCheck(SMB_TRANS2
,handle
)==SMB_SUCCESS
) {
2166 ptr
= handle
->message
.smb
;
2167 /*** Get parameter offset ***/
2168 pos
= getUShort(ptr
,(SMB_HEADER_SIZE
+9));
2169 sdir
->sid
= getUShort(ptr
, pos
); pos
+= 2;
2170 sess
->count
= getUShort(ptr
, pos
); pos
+= 2;
2171 sess
->eos
= getUShort(ptr
, pos
); pos
+= 2;
2172 pos
+= 2; // USHORT EaErrorOffset;
2173 pos
+= 2; // USHORT LastNameOffset;
2174 pos
+= 2; // padding?
2178 pos
+= 4; // ULONG NextEntryOffset;
2179 pos
+= 4; // ULONG FileIndex;
2180 sdir
->ctime
= getULongLong(ptr
, pos
) - handle
->session
.timeOffset
; pos
+= 8; // ULONGLONG - creation time
2181 sdir
->atime
= getULongLong(ptr
, pos
) - handle
->session
.timeOffset
; pos
+= 8; // ULONGLONG - access time
2182 sdir
->mtime
= getULongLong(ptr
, pos
) - handle
->session
.timeOffset
; pos
+= 8; // ULONGLONG - write time
2183 pos
+= 8; // ULONGLONG - change time low
2184 sdir
->size
= getULongLong(ptr
, pos
); pos
+= 8;
2186 sdir
->attributes
= getUInt(ptr
, pos
); pos
+= 4;
2187 len
=getUInt(ptr
, pos
);
2190 utf16_to_utf8(sdir
->name
,(char*)&ptr
[pos
],len
);
2192 strcpy(sdir
->name
,(const char*)&ptr
[pos
]);
2200 handle
->conn_valid
= false;
2207 s32
SMB_FindNext(SMBDIRENTRY
*sdir
,SMBCONN smbhndl
)
2216 handle
= __smb_handle_open(smbhndl
);
2217 if(!handle
) return SMB_ERROR
;
2219 sess
= &handle
->session
;
2220 if(sess
->eos
|| sdir
->sid
==0) return SMB_ERROR
;
2222 MakeSMBHeader(SMB_TRANS2
,CIFS_FLAGS1
,handle
->unicode
?CIFS_FLAGS2_UNICODE
:CIFS_FLAGS2
,handle
);
2223 MakeTRANS2Header(SMB_FIND_NEXT2
,handle
);
2225 bpos
= pos
= (T2_BYTE_CNT
+2);
2226 pos
+= 3; /*** Padding ***/
2227 ptr
= handle
->message
.smb
;
2228 setUShort(ptr
, pos
, sdir
->sid
);
2229 pos
+= 2; /*** Search ID ***/
2230 setUShort(ptr
, pos
, 1);
2231 pos
+= 2; /*** Count ***/
2232 setUShort(ptr
, pos
, 260); // SMB_FIND_FILE_BOTH_DIRECTORY_INFO
2233 pos
+= 2; /*** Level of Interest ***/
2234 pos
+= 4; /*** Storage Type == 0 ***/
2235 setUShort(ptr
, pos
, 12);
2236 pos
+=2; /*** Search flags ***/
2240 if(handle
->unicode
)pad
=1;
2243 /*** Update counts ***/
2244 setUShort(ptr
, T2_PRM_CNT
, 13+pad
);
2245 setUShort(ptr
, T2_SPRM_CNT
, 13+pad
);
2246 setUShort(ptr
, T2_SPRM_OFS
, 68);
2247 setUShort(ptr
, T2_BYTE_CNT
, (pos
-bpos
));
2249 handle
->message
.msg
= NBT_SESSISON_MSG
;
2250 handle
->message
.length
= htons(pos
);
2253 ret
= smb_send(handle
->sck_server
,(char*)&handle
->message
,pos
);
2254 if(ret
<0) goto failed
;
2257 if (SMBCheck(SMB_TRANS2
,handle
)==SMB_SUCCESS
) {
2258 ptr
= handle
->message
.smb
;
2259 /*** Get parameter offset ***/
2260 pos
= getUShort(ptr
,(SMB_HEADER_SIZE
+9));
2261 sess
->count
= getUShort(ptr
, pos
); pos
+= 2;
2262 sess
->eos
= getUShort(ptr
, pos
); pos
+= 2;
2263 pos
+= 2; // USHORT EaErrorOffset;
2264 pos
+= 2; // USHORT LastNameOffset;
2269 pos
+= 4; // ULONG NextEntryOffset;
2270 pos
+= 4; // ULONG FileIndex;
2271 sdir
->ctime
= getULongLong(ptr
, pos
) - handle
->session
.timeOffset
; pos
+= 8; // ULONGLONG - creation time
2272 sdir
->atime
= getULongLong(ptr
, pos
) - handle
->session
.timeOffset
; pos
+= 8; // ULONGLONG - access time
2273 sdir
->mtime
= getULongLong(ptr
, pos
) - handle
->session
.timeOffset
; pos
+= 8; // ULONGLONG - write time
2274 pos
+= 8; // ULONGLONG - change time low
2275 sdir
->size
= getULongLong(ptr
, pos
); pos
+= 8;
2277 sdir
->attributes
= getUInt(ptr
, pos
); pos
+= 4;
2278 len
=getUInt(ptr
, pos
);
2281 utf16_to_utf8(sdir
->name
, (char*)&ptr
[pos
],len
);
2283 strcpy (sdir
->name
, (const char*)&ptr
[pos
]);
2291 handle
->conn_valid
= false;
2298 s32
SMB_FindClose(SMBDIRENTRY
*sdir
,SMBCONN smbhndl
)
2305 handle
= __smb_handle_open(smbhndl
);
2306 if(!handle
) return SMB_ERROR
;
2308 if(sdir
->sid
==0) return SMB_ERROR
;
2310 MakeSMBHeader(SMB_FIND_CLOSE2
,CIFS_FLAGS1
,handle
->unicode
?CIFS_FLAGS2_UNICODE
:CIFS_FLAGS2
,handle
);
2312 pos
= SMB_HEADER_SIZE
;
2313 ptr
= handle
->message
.smb
;
2314 setUChar(ptr
, pos
, 1);
2315 pos
++; /*** Word Count ***/
2316 setUShort(ptr
, pos
, sdir
->sid
);
2318 pos
+= 2; /*** Byte Count ***/
2320 handle
->message
.msg
= NBT_SESSISON_MSG
;
2321 handle
->message
.length
= htons(pos
);
2324 ret
= smb_send(handle
->sck_server
,(char*)&handle
->message
,pos
);
2325 if(ret
<0) goto failed
;
2327 ret
= SMBCheck(SMB_FIND_CLOSE2
,handle
);
2331 handle
->conn_valid
= false;