Pārlūkot izejas kodu

Hide non public headers

Signed-off-by: Jean-Baptiste Kempf <jb@videolan.org>
Thomas Guillem 10 gadi atpakaļ
vecāks
revīzija
acb49695b5

+ 23 - 15
Makefile.am

@@ -18,29 +18,37 @@ bdsmdir = $(includedir)/bdsm
 
 bdsm_HEADERS = \
     include/bdsm.h      \
-    include/bdsm/debug.h    \
-    include/bdsm/hmac_md5.h   \
     include/bdsm/netbios_defs.h   \
     include/bdsm/netbios_ns.h   \
-    include/bdsm/netbios_query.h  \
-    include/bdsm/netbios_session.h  \
-    include/bdsm/netbios_utils.h  \
-    include/bdsm/smb_packets.h     \
     include/bdsm/smb_defs.h   \
-    include/bdsm/smb_fd.h    \
     include/bdsm/smb_file.h   \
-    include/bdsm/smb_message.h    \
-    include/bdsm/smb_ntlm.h   \
     include/bdsm/smb_session.h    \
     include/bdsm/smb_share.h    \
     include/bdsm/smb_stat.h   \
-    include/bdsm/smb_types.h    \
-    include/bdsm/smb_utils.h    \
-    include/bdsm/smb_buffer.h
+    include/bdsm/smb_types.h
 noinst_HEADERS = \
-    include/bdsm/smb_session_msg.h \
-    include/bdsm/smb_transport.h   \
-    include/bdsm/smb_spnego.h      \
+    src/bdsm_debug.h    \
+    src/hmac_md5.h   \
+    src/netbios_defs.h   \
+    src/netbios_ns.h   \
+    src/netbios_query.h  \
+    src/netbios_session.h  \
+    src/netbios_utils.h  \
+    src/smb_buffer.h \
+    src/smb_defs.h   \
+    src/smb_fd.h    \
+    src/smb_file.h   \
+    src/smb_message.h    \
+    src/smb_packets.h     \
+    src/smb_ntlm.h   \
+    src/smb_session.h    \
+    src/smb_share.h    \
+    src/smb_stat.h   \
+    src/smb_session_msg.h \
+    src/smb_spnego.h      \
+    src/smb_types.h    \
+    src/smb_transport.h   \
+    src/smb_utils.h    \
     contrib/spnego/spnego_asn1.h   \
     contrib/mdx/md4.h              \
     contrib/mdx/md5.h              \

+ 1 - 1
include/bdsm.h

@@ -24,7 +24,7 @@
 #define BDSM_VERSION_AGE      0
 
 #include "bdsm/netbios_ns.h"
-#include "bdsm/netbios_session.h"
+#include "bdsm/netbios_defs.h"
 #include "bdsm/smb_session.h"
 #include "bdsm/smb_share.h"
 #include "bdsm/smb_file.h"

+ 2 - 49
include/bdsm/netbios_defs.h

@@ -16,60 +16,13 @@
 // published by Sam Hocevar. See the COPYING file for more details.
 //----------------------------------------------------------------------------
 
-#ifndef __BDSM_NETBIOS_DEFS_H_
-#define __BDSM_NETBIOS_DEFS_H_
-
-#include <stdint.h>
-
-#define NETBIOS_PORT_NAME     137 // UDP
-#define NETBIOS_PORT_SESSION  139 // TCP
-#define NETBIOS_PORT_DIRECT   445 // TCP
-
-#define NETBIOS_NAME_LENGTH   15
+#ifndef _BDSM_NETBIOS_DEFS_H_
+#define _BDSM_NETBIOS_DEFS_H_
 
 // Netbios name types
 #define NETBIOS_WORKSTATION   0x00
 #define NETBIOS_MESSENGER     0x03
 #define NETBIOS_FILESERVER    0x20
 #define NETBIOS_DOMAINMASTER  0x1b
-// http://ubiqx.org/cifs/rfc-draft/rfc1001.html#s17.2
-#define NETBIOS_WILDCARD      { 32, 'C', 'K', 'A', 'A', 'A', 'A', 'A', 'A',    \
-    'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', \
-    'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 0 }
-
-#define NETBIOS_FLAG_QUERY      (1 << 15)
-#define NETBIOS_FLAG_TRUNCATED  (1 << 9)
-#define NETBIOS_FLAG_RECURSIVE  (1 << 8)
-#define NETBIOS_FLAG_BROADCAST  (1 << 4)
-
-// Name Service Query
-#define NETBIOS_OP_NAME_QUERY         0x00
-// Session Service
-#define NETBIOS_OP_SESSION_MSG        0x00
-#define NETBIOS_OP_SESSION_REQ        0x81
-#define NETBIOS_OP_SESSION_REQ_OK     0x82
-#define NETBIOS_OP_SESSION_REQ_NOK    0x83
-#define NETBIOS_OP_SESSION_RETARGET   0x84
-#define NETBIOS_OP_SESSION_KEEPALIVE  0x85
-
-typedef struct
-{
-    uint16_t                    trn_id;     // Transaction ID
-    uint16_t                    flags;      // Various flags
-    uint16_t                    queries;    // Number of queries in this packet
-    uint16_t                    answers;    // Number of answers
-    uint16_t                    ns_count;   // Number of authorities (?)
-    uint16_t                    ar_count;   // Additionnal (??)
-    char                        payload[];
-} __attribute__((packed))   netbios_query_packet;
-
-typedef struct
-{
-    uint8_t                     opcode;     // 'TYPE'
-    uint8_t                     flags;      // 0-6 reserved (== 0), byte 7 is the
-    // beginning of the length field (!!)
-    uint16_t                    length;     // payload length;
-    uint8_t                     payload[];
-} __attribute__((packed))   netbios_session_packet;
 
 #endif

+ 3 - 61
include/bdsm/netbios_ns.h

@@ -19,31 +19,15 @@
 #ifndef __BDSM_NETBIOS_NS_H_
 #define __BDSM_NETBIOS_NS_H_
 
-#include "bdsm/netbios_defs.h"
-#include "bdsm/netbios_query.h"
-
-#include <sys/socket.h>
-#include <netinet/ip.h>
-#include <netinet/udp.h>
+#include <stdint.h>
 
 /**
  * @file netbios_ns.h
  * @brief Netbios name service
  */
 
-/**
- * @brief Represents an correspondance between an IP address and a Netbios name.
- *
- * @details Consider it as an opaque data structure whose internal layout might
- * change at any time, please use the provided accessors functions
- */
-typedef struct                netbios_ns_entry_s
-{
-    struct netbios_ns_entry_s     *next;
-    struct in_addr                address;
-    char                          name[NETBIOS_NAME_LENGTH + 1];
-    char                          type;
-}                             netbios_ns_entry;
+typedef struct netbios_ns_entry netbios_ns_entry;
+typedef struct netbios_ns netbios_ns;
 
 /**
  * @brief Get the name of the entry referenced by the iterator iter.
@@ -71,22 +55,6 @@ uint32_t            netbios_ns_entry_ip(netbios_ns_entry *entry);
 char                netbios_ns_entry_type(netbios_ns_entry *entry);
 
 /**
- * @brief The netbios name service object.
- *
- * @details Holds all the necessary data structure to perform resolution and
- * discovery, and to stores the results. Consider it as an opaque data
- * structure, use the related functions to interact with it.
- */
-
-typedef struct
-{
-    int                 socket;
-    struct sockaddr_in  addr;
-    uint16_t            last_trn_id;  // Last transaction id used;
-    netbios_ns_entry  *entries;     // NS entries cache, mainly used by discover()
-}                   netbios_ns;
-
-/**
  * @brief Allocate and initialize the Netbios name service client object.
  * @return A newly allocated netbios_ns ready for querying.
  * Deallocate with netbios_ns_destroy().
@@ -171,30 +139,4 @@ const char          *netbios_ns_inverse(netbios_ns *ns, uint32_t ip);
  */
 void                netbios_ns_clear(netbios_ns *ns);
 
-/**
- * @internal
- * @brief Add an entry to the name service list.
- * @details You can provide a name and/or an ip
- *
- * @param ns The name service object.
- * @param name The ASCII name of the entry or NULL
- * @param type the <X> type for this entry or -1
- * @param ip The IP address in network byte order (or 0)
- * @return The added entry
- */
-netbios_ns_entry    *netbios_ns_entry_add(netbios_ns *ns, const char *name,
-        char type, uint32_t ip);
-/**
- * @internal
- * @brief Find an entry in
- * @details [long description]
- *
- * @param ns [description]
- * @param by_name [description]
- * @param ip [description]
- * @return [description]
- */
-netbios_ns_entry    *netbios_ns_entry_find(netbios_ns *ns, const char *by_name,
-        uint32_t ip);
-
 #endif

+ 0 - 2
include/bdsm/smb_defs.h

@@ -24,8 +24,6 @@
 #ifndef __BSDM_SMB_DEFS_H_
 #define __BSDM_SMB_DEFS_H_
 
-#include <bdsm/smb_packets.h>
-
 #define SMB_DEFAULT_BUFSIZE     (8192)
 
 enum

+ 0 - 31
include/bdsm/smb_session.h

@@ -20,11 +20,8 @@
 #define __BDSM_SMB_SESSION_H_
 
 
-#include "bdsm/smb_buffer.h"
-#include "bdsm/netbios_session.h"
 #include "bdsm/smb_defs.h"
 #include "bdsm/smb_types.h"
-#include "bdsm/smb_message.h"
 
 /**
  * @file smb_session.h
@@ -32,34 +29,6 @@
  */
 
 /**
- * @internal
- * @brief Get a smb_tid from a smb_fd
- *
- * @param fd a smb_fd
- * @return A smb_tid
- */
-#define SMB_FD_TID(fd)    ((smb_tid)(fd >> 16))
-/**
- * @internal
- * @brief Get a smb_fid from a smb_fd
- *
- * @param fd a smb_fid
- * @return A smb_fid
- */
-#define SMB_FD_FID(fd)    ((smb_fid)(fd & 0x0000ffff))
-/**
- * @internal
- * @brief Compute the smb_fd for the given smb_tid and smb_fid
- * @param tid a smb_tid
- * @param fid a smb_fid *
- * @return A smb_fd
- */
-#define SMB_FD(tid, fid)  ((((smb_fd)tid) << 16) | (((smb_fd) fid)))
-
-/* Our reception buffer grows as necessary, so we can put the max here */
-#define SMB_SESSION_MAX_BUFFER (0xffff)
- 
-/**
  * @brief Allocates a new Session object
  * @details To be able to perform actions on shares and file, you'll need to
  * call smb_session_connect, then authenticate with smb_authenticate.

+ 2 - 2
include/bdsm/smb_stat.h

@@ -21,8 +21,8 @@
  * @brief File status / listing
  */
 
-#ifndef __BDSM_SMB_TRANS2_H_
-#define __BDSM_SMB_TRANS2_H_
+#ifndef __BDSM_SMB_STAT_H_
+#define __BDSM_SMB_STAT_H_
 
 #include "bdsm/smb_defs.h"
 #include "bdsm/smb_session.h"

+ 6 - 90
include/bdsm/smb_types.h

@@ -29,9 +29,6 @@
 #include <libtasn1.h>
 #include <stdbool.h>
 
-#include "bdsm/smb_buffer.h"
-#include "bdsm/smb_packets.h"
-
 /**
   * @struct smb_tid
   * @brief The id of a connection to a share within a session.
@@ -52,51 +49,6 @@ typedef uint16_t    smb_fid;
   */
 typedef uint32_t    smb_fd;
 
-/**
- * @internal
- * @struct smb_file
- * @brief An opaque data structure to represent file
- */
-typedef struct  smb_file_s
-{
-    struct smb_file_s   *next;          // Next file in this share
-    char                *name;
-    smb_fid             fid;
-    smb_tid             tid;
-    size_t              name_len;
-    uint64_t            created;
-    uint64_t            accessed;
-    uint64_t            written;
-    uint64_t            changed;
-    uint64_t            alloc_size;
-    uint64_t            size;
-    uint32_t            attr;
-    uint32_t            readp;          // Current read pointer (position);
-    int                 is_dir;         // 0 -> file, 1 -> directory
-} smb_file;
-
-typedef struct smb_share_s
-{
-    struct smb_share_s  *next;          // Next share in this session
-    struct smb_file_s   *files;         // List of all open files for this share
-    smb_tid             tid;
-    uint16_t            opts;           // Optionnal support opts
-    uint16_t            rights;         // Maximum rights field
-    uint16_t            guest_rights;
-} smb_share;
-
-typedef struct smb_transport_s
-{
-    void              *session;
-    void              *(*new)(size_t buf_size);
-    int               (*connect)(struct in_addr *addr, void *s, const char *name);
-    void              (*destroy)(void *s);
-    void              (*pkt_init)(void *s);
-    int               (*pkt_append)(void *s, void *data, size_t size);
-    int               (*send)(void *s);
-    ssize_t           (*recv)(void *s, void **data);
-}                   smb_transport;
-
 // An structure to store user credentials;
 // login:password@domain (also DOMAIN\login)
 typedef struct
@@ -106,38 +58,10 @@ typedef struct
     char     *password;
 }           smb_creds;
 
-typedef struct
-{
-    char                name[16];       // The server name
-    uint16_t            dialect;        // The selected dialect
-    uint16_t            security_mode;  // Security mode
-    uint16_t            uid;            // uid attributed by the server.
-    uint32_t            session_key;    // The session key sent by the server on protocol negotiate
-    uint32_t            caps;           // Server caps replyed during negotiate
-    uint64_t            challenge;      // For challenge response security
-    uint64_t            ts;             // It seems Win7 requires it :-/
-}                   smb_srv_info;
-
 /**
  * @brief An opaque data structure to represent a SMB Session.
  */
-typedef struct
-{
-    int                 state;
-    bool                guest;            // Are we logged as guest ?
-
-    // Informations about the smb server we are connected to.
-    smb_srv_info        srv;
-
-
-    ASN1_TYPE           spnego_asn1;
-    smb_buffer          xsec_target;
-
-    smb_creds           creds;
-    smb_transport       transport;
-
-    struct smb_share_s  *shares;          // shares->files | Map fd <-> smb_file
-}                   smb_session;
+typedef struct smb_session smb_session;
 
 /**
  * @struct smb_share_list
@@ -146,6 +70,11 @@ typedef struct
 typedef char  **smb_share_list;
 
 /**
+ * @brief An opaque data structure to represent file
+ */
+typedef struct smb_file smb_file;
+
+/**
  * @struct smb_stat_list
  * @brief An opaque structure containing a list of file status
  */
@@ -157,17 +86,4 @@ typedef smb_file *smb_stat_list;
  */
 typedef smb_file *smb_stat;
 
-/**
- * @internal
- * @struct smb_message
- * @brief A convenience structure used to build smb messages
- */
-typedef struct
-{
-    size_t          payload_size; // Size of the allocated payload
-    size_t          cursor;       // Write cursor in the payload
-    smb_packet      *packet;      // Yummy yummy, Fruity fruity !
-}                               smb_message;
-
-
 #endif

+ 2 - 2
include/bdsm/debug.h

@@ -16,8 +16,8 @@
 // published by Sam Hocevar. See the COPYING file for more details.
 //----------------------------------------------------------------------------
 
-#ifndef __BDSM_DEBUG_H__
-# define __BDSM_DEBUG_H__
+#ifndef _BDSM_DEBUG_H_
+# define _BDSM_DEBUG_H_
 
 # include "config.h"
 

+ 1 - 1
src/hmac_md5.c

@@ -20,7 +20,7 @@
 #include <stdlib.h>
 #include <string.h>
 
-#include "bdsm/hmac_md5.h"
+#include "hmac_md5.h"
 
 unsigned char *HMAC_MD5(const void *key, size_t key_len, const void *msg,
                         size_t msg_len, void *hmac)

+ 2 - 2
include/bdsm/hmac_md5.h

@@ -16,8 +16,8 @@
 // published by Sam Hocevar. See the COPYING file for more details.
 //----------------------------------------------------------------------------
 
-#ifndef __BDSM_SMB_SESSION_H_
-#define __BDSM_SMB_SESSION_H_
+#ifndef _HMAC_MD5_H_
+#define _HMAC_MD5_H_
 
 #include <stdint.h>
 #include <mdx/md5.h>

+ 72 - 0
src/netbios_defs.h

@@ -0,0 +1,72 @@
+//---------------------------------------------------------------------------
+//  __________________    _________  _____            _____  .__         ._.
+//  \______   \______ \  /   _____/ /     \          /  _  \ |__| ____   | |
+//   |    |  _/|    |  \ \_____  \ /  \ /  \        /  /_\  \|  _/ __ \  | |
+//   |    |   \|    `   \/        /    Y    \      /    |    |  \  ___/   \|
+//   |______  /_______  /_______  \____|__  / /\   \____|__  |__|\___ |   __
+//          \/        \/        \/        \/  )/           \/        \/   \/
+//
+// This file is part of libdsm. Copyright © 2014 VideoLabs SAS
+//
+// Author: Julien 'Lta' BALLET <contact@lta.io>
+//
+// This program is free software. It comes without any warranty, to the extent
+// permitted by applicable law. You can redistribute it and/or modify it under
+// the terms of the Do What The Fuck You Want To Public License, Version 2, as
+// published by Sam Hocevar. See the COPYING file for more details.
+//----------------------------------------------------------------------------
+
+#ifndef _NETBIOS_DEFS_H_
+#define _NETBIOS_DEFS_H_
+
+#include <stdint.h>
+
+#include "bdsm/netbios_defs.h"
+
+#define NETBIOS_PORT_NAME     137 // UDP
+#define NETBIOS_PORT_SESSION  139 // TCP
+#define NETBIOS_PORT_DIRECT   445 // TCP
+
+#define NETBIOS_NAME_LENGTH   15
+
+// http://ubiqx.org/cifs/rfc-draft/rfc1001.html#s17.2
+#define NETBIOS_WILDCARD      { 32, 'C', 'K', 'A', 'A', 'A', 'A', 'A', 'A',    \
+    'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', \
+    'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 0 }
+
+#define NETBIOS_FLAG_QUERY      (1 << 15)
+#define NETBIOS_FLAG_TRUNCATED  (1 << 9)
+#define NETBIOS_FLAG_RECURSIVE  (1 << 8)
+#define NETBIOS_FLAG_BROADCAST  (1 << 4)
+
+// Name Service Query
+#define NETBIOS_OP_NAME_QUERY         0x00
+// Session Service
+#define NETBIOS_OP_SESSION_MSG        0x00
+#define NETBIOS_OP_SESSION_REQ        0x81
+#define NETBIOS_OP_SESSION_REQ_OK     0x82
+#define NETBIOS_OP_SESSION_REQ_NOK    0x83
+#define NETBIOS_OP_SESSION_RETARGET   0x84
+#define NETBIOS_OP_SESSION_KEEPALIVE  0x85
+
+typedef struct
+{
+    uint16_t                    trn_id;     // Transaction ID
+    uint16_t                    flags;      // Various flags
+    uint16_t                    queries;    // Number of queries in this packet
+    uint16_t                    answers;    // Number of answers
+    uint16_t                    ns_count;   // Number of authorities (?)
+    uint16_t                    ar_count;   // Additionnal (??)
+    char                        payload[];
+} __attribute__((packed))   netbios_query_packet;
+
+typedef struct
+{
+    uint8_t                     opcode;     // 'TYPE'
+    uint8_t                     flags;      // 0-6 reserved (== 0), byte 7 is the
+    // beginning of the length field (!!)
+    uint16_t                    length;     // payload length;
+    uint8_t                     payload[];
+} __attribute__((packed))   netbios_session_packet;
+
+#endif

+ 4 - 4
src/netbios_ns.c

@@ -29,10 +29,10 @@
 #include <arpa/inet.h>
 #include <sys/select.h>
 
-#include "bdsm/debug.h"
-#include "bdsm/netbios_ns.h"
-#include "bdsm/netbios_query.h"
-#include "bdsm/netbios_utils.h"
+#include "bdsm_debug.h"
+#include "netbios_ns.h"
+#include "netbios_query.h"
+#include "netbios_utils.h"
 
 
 static int    ns_open_socket(netbios_ns *ns)

+ 84 - 0
src/netbios_ns.h

@@ -0,0 +1,84 @@
+//---------------------------------------------------------------------------
+//  __________________    _________  _____            _____  .__         ._.
+//  \______   \______ \  /   _____/ /     \          /  _  \ |__| ____   | |
+//   |    |  _/|    |  \ \_____  \ /  \ /  \        /  /_\  \|  _/ __ \  | |
+//   |    |   \|    `   \/        /    Y    \      /    |    |  \  ___/   \|
+//   |______  /_______  /_______  \____|__  / /\   \____|__  |__|\___ |   __
+//          \/        \/        \/        \/  )/           \/        \/   \/
+//
+// This file is part of libdsm. Copyright © 2014 VideoLabs SAS
+//
+// Author: Julien 'Lta' BALLET <contact@lta.io>
+//
+// This program is free software. It comes without any warranty, to the extent
+// permitted by applicable law. You can redistribute it and/or modify it under
+// the terms of the Do What The Fuck You Want To Public License, Version 2, as
+// published by Sam Hocevar. See the COPYING file for more details.
+//----------------------------------------------------------------------------
+
+#ifndef _NETBIOS_NS_H_
+#define _NETBIOS_NS_H_
+
+#include <bdsm/netbios_ns.h>
+
+#include <sys/socket.h>
+#include <netinet/ip.h>
+#include <netinet/udp.h>
+
+#include "netbios_defs.h"
+#include "netbios_query.h"
+
+/**
+ * @file netbios_ns.h
+ * @brief Netbios name service
+ */
+
+/**
+ * @brief Represents an correspondance between an IP address and a Netbios name.
+ *
+ * @details Consider it as an opaque data structure whose internal layout might
+ * change at any time, please use the provided accessors functions
+ */
+struct netbios_ns_entry
+{
+    netbios_ns_entry              *next;
+    struct in_addr                address;
+    char                          name[NETBIOS_NAME_LENGTH + 1];
+    char                          type;
+};
+
+struct netbios_ns
+{
+    int                 socket;
+    struct sockaddr_in  addr;
+    uint16_t            last_trn_id;  // Last transaction id used;
+    netbios_ns_entry    *entries;     // NS entries cache, mainly used by discover()
+};
+
+/**
+ * @internal
+ * @brief Add an entry to the name service list.
+ * @details You can provide a name and/or an ip
+ *
+ * @param ns The name service object.
+ * @param name The ASCII name of the entry or NULL
+ * @param type the <X> type for this entry or -1
+ * @param ip The IP address in network byte order (or 0)
+ * @return The added entry
+ */
+netbios_ns_entry    *netbios_ns_entry_add(netbios_ns *ns, const char *name,
+        char type, uint32_t ip);
+/**
+ * @internal
+ * @brief Find an entry in
+ * @details [long description]
+ *
+ * @param ns [description]
+ * @param by_name [description]
+ * @param ip [description]
+ * @return [description]
+ */
+netbios_ns_entry    *netbios_ns_entry_find(netbios_ns *ns, const char *by_name,
+        uint32_t ip);
+
+#endif

+ 1 - 1
src/netbios_ns_entry.c

@@ -21,7 +21,7 @@
 #include <string.h>
 #include <assert.h>
 
-#include "bdsm/netbios_ns.h"
+#include "netbios_ns.h"
 
 const char          *netbios_ns_entry_name(netbios_ns_entry *entry)
 {

+ 1 - 1
src/netbios_query.c

@@ -22,7 +22,7 @@
 #include <assert.h>
 #include <arpa/inet.h>
 
-#include "bdsm/netbios_query.h"
+#include "netbios_query.h"
 
 netbios_query   *netbios_query_new(size_t payload_size,
                                    int is_query, char opcode)

+ 3 - 3
include/bdsm/netbios_query.h

@@ -16,13 +16,13 @@
 // published by Sam Hocevar. See the COPYING file for more details.
 //----------------------------------------------------------------------------
 
-#ifndef __BDSM_NETBIOS_QUERY_H_
-#define __BDSM_NETBIOS_QUERY_H_
+#ifndef _NETBIOS_QUERY_H_
+#define _NETBIOS_QUERY_H_
 
 #include <stdint.h>
 #include <stddef.h>
 
-#include "bdsm/netbios_defs.h"
+#include "netbios_defs.h"
 
 typedef struct              netbios_query_s
 {

+ 3 - 3
src/netbios_session.c

@@ -26,9 +26,9 @@
 #include <sys/socket.h>
 #include <unistd.h>
 
-#include "bdsm/debug.h"
-#include "bdsm/netbios_session.h"
-#include "bdsm/netbios_utils.h"
+#include "bdsm_debug.h"
+#include "netbios_session.h"
+#include "netbios_utils.h"
 
 static int        open_socket_and_connect(netbios_session *s)
 {

+ 3 - 3
include/bdsm/netbios_session.h

@@ -16,8 +16,8 @@
 // published by Sam Hocevar. See the COPYING file for more details.
 //----------------------------------------------------------------------------
 
-#ifndef __BDSM_NETBIOS_SESSION_H_
-#define __BDSM_NETBIOS_SESSION_H_
+#ifndef _NETBIOS_SESSION_H_
+#define _NETBIOS_SESSION_H_
 
 #include <stdint.h>
 #include <stddef.h>
@@ -25,7 +25,7 @@
 #include <sys/socket.h>
 #include <netinet/in.h>
 
-#include "bdsm/netbios_defs.h"
+#include "netbios_defs.h"
 
 #define NETBIOS_SESSION_NEW         0
 #define NETBIOS_SESSION_CONNECTING  1

+ 1 - 1
src/netbios_utils.c

@@ -20,7 +20,7 @@
 #include <ctype.h>
 #include <stdlib.h>
 
-#include "bdsm/netbios_utils.h"
+#include "netbios_utils.h"
 
 static short  nibble_encode(char c)
 {

+ 2 - 2
include/bdsm/netbios_utils.h

@@ -16,8 +16,8 @@
 // published by Sam Hocevar. See the COPYING file for more details.
 //----------------------------------------------------------------------------
 
-#ifndef __BDSM_NETBIOS_UTILS_H_
-#define __BDSM_NETBIOS_UTILS_H_
+#ifndef _NETBIOS_UTILS_H_
+#define _NETBIOS_UTILS_H_
 
 #include "netbios_defs.h"
 

+ 1 - 1
src/smb_buffer.c

@@ -20,7 +20,7 @@
 #include <assert.h>
 #include <stdlib.h>
 
-#include "bdsm/smb_buffer.h"
+#include "smb_buffer.h"
 
 void    smb_buffer_init(smb_buffer *buf, void *data, size_t size)
 {

+ 2 - 2
include/bdsm/smb_buffer.h

@@ -21,8 +21,8 @@
  * @brief liBDSM types and structures
  */
 
-#ifndef __BDSM_SMB_BUFFER_H_
-#define __BDSM_SMB_BUFFER_H_
+#ifndef _SMB_BUFFER_H_
+#define _SMB_BUFFER_H_
 
 #include <stdlib.h>
 

+ 29 - 0
src/smb_defs.h

@@ -0,0 +1,29 @@
+//---------------------------------------------------------------------------
+//  __________________    _________  _____            _____  .__         ._.
+//  \______   \______ \  /   _____/ /     \          /  _  \ |__| ____   | |
+//   |    |  _/|    |  \ \_____  \ /  \ /  \        /  /_\  \|  _/ __ \  | |
+//   |    |   \|    `   \/        /    Y    \      /    |    |  \  ___/   \|
+//   |______  /_______  /_______  \____|__  / /\   \____|__  |__|\___ |   __
+//          \/        \/        \/        \/  )/           \/        \/   \/
+//
+// This file is part of libdsm. Copyright © 2014 VideoLabs SAS
+//
+// Author: Julien 'Lta' BALLET <contact@lta.io>
+//
+// This program is free software. It comes without any warranty, to the extent
+// permitted by applicable law. You can redistribute it and/or modify it under
+// the terms of the Do What The Fuck You Want To Public License, Version 2, as
+// published by Sam Hocevar. See the COPYING file for more details.
+//----------------------------------------------------------------------------
+
+/**
+ * @file smb_defs.h
+ * @brief SMB usefull constants
+ */
+
+#ifndef _SMB_DEFS_H_
+#define _SMB_DEFS_H_
+
+#include "bdsm/smb_defs.h"
+
+#endif

+ 1 - 1
src/smb_fd.c

@@ -18,7 +18,7 @@
 
 #include <assert.h>
 
-#include "bdsm/smb_fd.h"
+#include "smb_fd.h"
 
 void        smb_session_share_add(smb_session *s, smb_share *share)
 {

+ 4 - 3
include/bdsm/smb_fd.h

@@ -16,10 +16,11 @@
 // published by Sam Hocevar. See the COPYING file for more details.
 //----------------------------------------------------------------------------
 
-#ifndef __BDSM_SMB_FD_H_
-#define __BDSM_SMB_FD_H_
+#ifndef _SMB_FD_H_
+#define _SMB_FD_H_
 
-#include "bdsm/smb_session.h"
+#include "smb_session.h"
+#include "smb_message.h"
 
 void            smb_session_share_add(smb_session *s, smb_share *share);
 smb_share       *smb_session_share_get(smb_session *s, smb_tid tid);

+ 4 - 4
src/smb_file.c

@@ -22,10 +22,10 @@
 #include <string.h>
 #include <stdio.h>
 
-#include "bdsm/smb_session_msg.h"
-#include "bdsm/smb_fd.h"
-#include "bdsm/smb_utils.h"
-#include "bdsm/smb_file.h"
+#include "smb_session_msg.h"
+#include "smb_fd.h"
+#include "smb_utils.h"
+#include "smb_file.h"
 
 smb_fd      smb_fopen(smb_session *s, smb_tid tid, const char *path,
                       uint32_t o_flags)

+ 29 - 0
src/smb_file.h

@@ -0,0 +1,29 @@
+//---------------------------------------------------------------------------
+//  __________________    _________  _____            _____  .__         ._.
+//  \______   \______ \  /   _____/ /     \          /  _  \ |__| ____   | |
+//   |    |  _/|    |  \ \_____  \ /  \ /  \        /  /_\  \|  _/ __ \  | |
+//   |    |   \|    `   \/        /    Y    \      /    |    |  \  ___/   \|
+//   |______  /_______  /_______  \____|__  / /\   \____|__  |__|\___ |   __
+//          \/        \/        \/        \/  )/           \/        \/   \/
+//
+// This file is part of libdsm. Copyright © 2014 VideoLabs SAS
+//
+// Author: Julien 'Lta' BALLET <contact@lta.io>
+//
+// This program is free software. It comes without any warranty, to the extent
+// permitted by applicable law. You can redistribute it and/or modify it under
+// the terms of the Do What The Fuck You Want To Public License, Version 2, as
+// published by Sam Hocevar. See the COPYING file for more details.
+//----------------------------------------------------------------------------
+
+/**
+ * @file smb_file.h
+ * @brief File operations
+ */
+
+#ifndef _SMB_FILE_H_
+#define _SMB_FILE_H_
+
+#include "bdsm/smb_file.h"
+
+#endif

+ 4 - 4
src/smb_message.c

@@ -23,10 +23,10 @@
 #include <sys/types.h>
 #include <unistd.h>
 
-#include "bdsm/debug.h"
-#include "bdsm/netbios_session.h"
-#include "bdsm/smb_message.h"
-#include "bdsm/smb_utils.h"
+#include "bdsm_debug.h"
+#include "netbios_session.h"
+#include "smb_message.h"
+#include "smb_utils.h"
 
 smb_message   *smb_message_new(uint8_t cmd, size_t payload_size)
 {

+ 4 - 4
include/bdsm/smb_message.h

@@ -16,11 +16,11 @@
 // published by Sam Hocevar. See the COPYING file for more details.
 //----------------------------------------------------------------------------
 
-#ifndef __BDSM_SMB_MESSAGE_H_
-#define __BDSM_SMB_MESSAGE_H_
+#ifndef _SMB_MESSAGE_H_
+#define _SMB_MESSAGE_H_
 
-#include "bdsm/smb_defs.h"
-#include "bdsm/smb_types.h"
+#include "smb_defs.h"
+#include "smb_types.h"
 
 smb_message     *smb_message_new(uint8_t cmd, size_t payload_size);
 smb_message     *smb_message_grow(smb_message *msg, size_t size);

+ 4 - 4
src/smb_ntlm.c

@@ -34,10 +34,10 @@
 
 #include "mdx/md4.h"
 #include "rc4/rc4.h"
-#include "bdsm/debug.h"
-#include "bdsm/hmac_md5.h"
-#include "bdsm/smb_utils.h"
-#include "bdsm/smb_ntlm.h"
+#include "bdsm_debug.h"
+#include "hmac_md5.h"
+#include "smb_utils.h"
+#include "smb_ntlm.h"
 
 uint64_t    smb_ntlm_generate_challenge()
 {

+ 4 - 4
include/bdsm/smb_ntlm.h

@@ -16,11 +16,11 @@
 // published by Sam Hocevar. See the COPYING file for more details.
 //----------------------------------------------------------------------------
 
-#ifndef __BDSM_SMB_NTLM_H_
-#define __BDSM_SMB_NTLM_H_
+#ifndef _SMB_NTLM_H_
+#define _SMB_NTLM_H_
 
-#include "bdsm/smb_buffer.h"
-#include "bdsm/smb_defs.h"
+#include "smb_defs.h"
+#include "smb_buffer.h"
 
 #define SMB_LM2_BLOB_SIZE         8
 #define SMB_NTLM_HASH_SIZE        16

+ 2 - 2
include/bdsm/smb_packets.h

@@ -16,8 +16,8 @@
 // published by Sam Hocevar. See the COPYING file for more details.
 //----------------------------------------------------------------------------
 
-#ifndef __BSDM_SMB_PACKETS_H_
-#define __BSDM_SMB_PACKETS_H_
+#ifndef _SMB_PACKETS_H_
+#define _SMB_PACKETS_H_
 
 #include <stdint.h>
 

+ 7 - 7
src/smb_session.c

@@ -21,13 +21,13 @@
 #include <stdio.h>
 #include <assert.h>
 
-#include "bdsm/debug.h"
-#include "bdsm/smb_session.h"
-#include "bdsm/smb_session_msg.h"
-#include "bdsm/smb_fd.h"
-#include "bdsm/smb_ntlm.h"
-#include "bdsm/smb_spnego.h"
-#include "bdsm/smb_transport.h"
+#include "bdsm_debug.h"
+#include "smb_session.h"
+#include "smb_session_msg.h"
+#include "smb_fd.h"
+#include "smb_ntlm.h"
+#include "smb_spnego.h"
+#include "smb_transport.h"
 
 static int        smb_negotiate(smb_session *s);
 

+ 57 - 0
src/smb_session.h

@@ -0,0 +1,57 @@
+//---------------------------------------------------------------------------
+//  __________________    _________  _____            _____  .__         ._.
+//  \______   \______ \  /   _____/ /     \          /  _  \ |__| ____   | |
+//   |    |  _/|    |  \ \_____  \ /  \ /  \        /  /_\  \|  _/ __ \  | |
+//   |    |   \|    `   \/        /    Y    \      /    |    |  \  ___/   \|
+//   |______  /_______  /_______  \____|__  / /\   \____|__  |__|\___ |   __
+//          \/        \/        \/        \/  )/           \/        \/   \/
+//
+// This file is part of libdsm. Copyright © 2014 VideoLabs SAS
+//
+// Author: Julien 'Lta' BALLET <contact@lta.io>
+//
+// This program is free software. It comes without any warranty, to the extent
+// permitted by applicable law. You can redistribute it and/or modify it under
+// the terms of the Do What The Fuck You Want To Public License, Version 2, as
+// published by Sam Hocevar. See the COPYING file for more details.
+//----------------------------------------------------------------------------
+
+#ifndef _SMB_SESSION_H_
+#define _SMB_SESSION_H_
+
+#include "bdsm/smb_session.h"
+
+/**
+ * @file smb_session.h
+ * @brief Functions to connect and authenticate to an SMB server
+ */
+
+/**
+ * @internal
+ * @brief Get a smb_tid from a smb_fd
+ *
+ * @param fd a smb_fd
+ * @return A smb_tid
+ */
+#define SMB_FD_TID(fd)    ((smb_tid)(fd >> 16))
+/**
+ * @internal
+ * @brief Get a smb_fid from a smb_fd
+ *
+ * @param fd a smb_fid
+ * @return A smb_fid
+ */
+#define SMB_FD_FID(fd)    ((smb_fid)(fd & 0x0000ffff))
+/**
+ * @internal
+ * @brief Compute the smb_fd for the given smb_tid and smb_fid
+ * @param tid a smb_tid
+ * @param fid a smb_fid *
+ * @return A smb_fd
+ */
+#define SMB_FD(tid, fid)  ((((smb_fd)tid) << 16) | (((smb_fd) fid)))
+
+/* Our reception buffer grows as necessary, so we can put the max here */
+#define SMB_SESSION_MAX_BUFFER (0xffff)
+ 
+#endif

+ 2 - 1
src/smb_session_msg.c

@@ -18,7 +18,8 @@
 
 #include <assert.h>
 
-#include "bdsm/smb_session.h"
+#include "smb_session.h"
+#include "smb_message.h"
 
 int             smb_session_send_msg(smb_session *s, smb_message *msg)
 {

+ 3 - 3
include/bdsm/smb_session_msg.h

@@ -16,10 +16,10 @@
 // published by Sam Hocevar. See the COPYING file for more details.
 //----------------------------------------------------------------------------
 
-#ifndef __BDSM_SMB_SESSION_MSG_H_
-#define __BDSM_SMB_SESSION_MSG_H_
+#ifndef _SMB_SESSION_MSG_H_
+#define _SMB_SESSION_MSG_H_
 
-#include "bdsm/smb_types.h"
+#include "smb_types.h"
 
 // Send a smb message for the provided smb_session
 int             smb_session_send_msg(smb_session *s, smb_message *msg);

+ 6 - 6
src/smb_share.c

@@ -22,12 +22,12 @@
 #include <string.h>
 #include <stdio.h>
 
-#include "bdsm/debug.h"
-#include "bdsm/smb_session_msg.h"
-#include "bdsm/smb_utils.h"
-#include "bdsm/smb_fd.h"
-#include "bdsm/smb_share.h"
-#include "bdsm/smb_file.h"
+#include "bdsm_debug.h"
+#include "smb_session_msg.h"
+#include "smb_utils.h"
+#include "smb_fd.h"
+#include "smb_share.h"
+#include "smb_file.h"
 
 
 smb_tid         smb_tree_connect(smb_session *s, const char *name)

+ 24 - 0
src/smb_share.h

@@ -0,0 +1,24 @@
+//---------------------------------------------------------------------------
+//  __________________    _________  _____            _____  .__         ._.
+//  \______   \______ \  /   _____/ /     \          /  _  \ |__| ____   | |
+//   |    |  _/|    |  \ \_____  \ /  \ /  \        /  /_\  \|  _/ __ \  | |
+//   |    |   \|    `   \/        /    Y    \      /    |    |  \  ___/   \|
+//   |______  /_______  /_______  \____|__  / /\   \____|__  |__|\___ |   __
+//          \/        \/        \/        \/  )/           \/        \/   \/
+//
+// This file is part of libdsm. Copyright © 2014 VideoLabs SAS
+//
+// Author: Julien 'Lta' BALLET <contact@lta.io>
+//
+// This program is free software. It comes without any warranty, to the extent
+// permitted by applicable law. You can redistribute it and/or modify it under
+// the terms of the Do What The Fuck You Want To Public License, Version 2, as
+// published by Sam Hocevar. See the COPYING file for more details.
+//----------------------------------------------------------------------------
+
+#ifndef _SMB_SHARE_H_
+#define _SMB_SHARE_H_
+
+#include "bdsm/smb_share.h"
+
+#endif

+ 5 - 4
src/smb_spnego.c

@@ -22,10 +22,11 @@
 #include <assert.h>
 #include <inttypes.h>
 
-#include "bdsm/debug.h"
-#include "bdsm/smb_session.h"
-#include "bdsm/smb_session_msg.h"
-#include "bdsm/smb_ntlm.h"
+#include "bdsm_debug.h"
+#include "smb_session.h"
+#include "smb_session_msg.h"
+#include "smb_message.h"
+#include "smb_ntlm.h"
 #include "spnego/spnego_asn1.h"
 
 static const char spnego_oid[]  = "1.3.6.1.5.5.2";

+ 3 - 3
include/bdsm/smb_spnego.h

@@ -17,10 +17,10 @@
 //----------------------------------------------------------------------------
 
 
-#ifndef __BDSM_SMB_SPNEGO_H_
-#define __BDSM_SMB_SPNEGO_H_
+#ifndef __SMB_SPNEGO_H_
+#define __SMB_SPNEGO_H_
 
-#include "bdsm/smb_types.h"
+#include "smb_types.h"
 
 int             smb_session_login_spnego(smb_session *s, const char *domain,
         const char *user, const char *password);

+ 2 - 2
src/smb_stat.c

@@ -18,8 +18,8 @@
 
 #include <assert.h>
 
-#include "bdsm/smb_stat.h"
-#include "bdsm/smb_fd.h"
+#include "smb_stat.h"
+#include "smb_fd.h"
 
 smb_stat        smb_stat_fd(smb_session *s, smb_fd fd)
 {

+ 29 - 0
src/smb_stat.h

@@ -0,0 +1,29 @@
+//---------------------------------------------------------------------------
+//  __________________    _________  _____            _____  .__         ._.
+//  \______   \______ \  /   _____/ /     \          /  _  \ |__| ____   | |
+//   |    |  _/|    |  \ \_____  \ /  \ /  \        /  /_\  \|  _/ __ \  | |
+//   |    |   \|    `   \/        /    Y    \      /    |    |  \  ___/   \|
+//   |______  /_______  /_______  \____|__  / /\   \____|__  |__|\___ |   __
+//          \/        \/        \/        \/  )/           \/        \/   \/
+//
+// This file is part of libdsm. Copyright © 2014 VideoLabs SAS
+//
+// Author: Julien 'Lta' BALLET <contact@lta.io>
+//
+// This program is free software. It comes without any warranty, to the extent
+// permitted by applicable law. You can redistribute it and/or modify it under
+// the terms of the Do What The Fuck You Want To Public License, Version 2, as
+// published by Sam Hocevar. See the COPYING file for more details.
+//----------------------------------------------------------------------------
+
+/**
+ * @file smb_stat.h
+ * @brief File status / listing
+ */
+
+#ifndef _SMB_STAT_H_
+#define _SMB_STAT_H_
+
+#include "bdsm/smb_stat.h"
+
+#endif

+ 5 - 5
src/smb_trans2.c

@@ -21,11 +21,11 @@
 #include <stdio.h>
 #include <assert.h>
 
-#include "bdsm/debug.h"
-#include "bdsm/smb_message.h"
-#include "bdsm/smb_session_msg.h"
-#include "bdsm/smb_utils.h"
-#include "bdsm/smb_stat.h"
+#include "bdsm_debug.h"
+#include "smb_message.h"
+#include "smb_session_msg.h"
+#include "smb_utils.h"
+#include "smb_stat.h"
 
 static smb_file *smb_find_parse(smb_message *msg)
 {

+ 2 - 2
src/smb_transport.c

@@ -18,8 +18,8 @@
 
 #include <assert.h>
 
-#include "bdsm/smb_transport.h"
-#include "bdsm/netbios_session.h"
+#include "smb_transport.h"
+#include "netbios_session.h"
 
 // XXX: This can be simplified, since we have only one function that differs
 

+ 3 - 3
include/bdsm/smb_transport.h

@@ -16,10 +16,10 @@
 // published by Sam Hocevar. See the COPYING file for more details.
 //----------------------------------------------------------------------------
 
-#ifndef __BDSM_SMB_TRANSPORT_H_
-#define __BDSM_SMB_TRANSPORT_H_
+#ifndef __SMB_TRANSPORT_H_
+#define __SMB_TRANSPORT_H_
 
-#include "bdsm/smb_types.h"
+#include "smb_types.h"
 
 /**
  * @internal

+ 125 - 0
src/smb_types.h

@@ -0,0 +1,125 @@
+//---------------------------------------------------------------------------
+//  __________________    _________  _____            _____  .__         ._.
+//  \______   \______ \  /   _____/ /     \          /  _  \ |__| ____   | |
+//   |    |  _/|    |  \ \_____  \ /  \ /  \        /  /_\  \|  _/ __ \  | |
+//   |    |   \|    `   \/        /    Y    \      /    |    |  \  ___/   \|
+//   |______  /_______  /_______  \____|__  / /\   \____|__  |__|\___ |   __
+//          \/        \/        \/        \/  )/           \/        \/   \/
+//
+// This file is part of libdsm. Copyright © 2014 VideoLabs SAS
+//
+// Author: Julien 'Lta' BALLET <contact@lta.io>
+//
+// This program is free software. It comes without any warranty, to the extent
+// permitted by applicable law. You can redistribute it and/or modify it under
+// the terms of the Do What The Fuck You Want To Public License, Version 2, as
+// published by Sam Hocevar. See the COPYING file for more details.
+//----------------------------------------------------------------------------
+
+/**
+ * @file smb_types.h
+ * @brief liBDSM types and structures
+ */
+
+#ifndef _SMB_TYPES_H_
+#define _SMB_TYPES_H_
+
+#include <netinet/ip.h>
+#include <stddef.h>
+#include <libtasn1.h>
+#include <stdbool.h>
+
+#include "bdsm/smb_types.h"
+#include "smb_buffer.h"
+#include "smb_packets.h"
+
+/**
+ * @internal
+ * @struct smb_file
+ * @brief An opaque data structure to represent file
+ */
+struct smb_file
+{
+    smb_file            *next;          // Next file in this share
+    char                *name;
+    smb_fid             fid;
+    smb_tid             tid;
+    size_t              name_len;
+    uint64_t            created;
+    uint64_t            accessed;
+    uint64_t            written;
+    uint64_t            changed;
+    uint64_t            alloc_size;
+    uint64_t            size;
+    uint32_t            attr;
+    uint32_t            readp;          // Current read pointer (position);
+    int                 is_dir;         // 0 -> file, 1 -> directory
+};
+
+typedef struct smb_share smb_share;
+struct smb_share
+{
+    smb_share           *next;          // Next share in this session
+    smb_file            *files;         // List of all open files for this share
+    smb_tid             tid;
+    uint16_t            opts;           // Optionnal support opts
+    uint16_t            rights;         // Maximum rights field
+    uint16_t            guest_rights;
+};
+
+typedef struct smb_transport smb_transport;
+struct smb_transport
+{
+    void              *session;
+    void              *(*new)(size_t buf_size);
+    int               (*connect)(struct in_addr *addr, void *s, const char *name);
+    void              (*destroy)(void *s);
+    void              (*pkt_init)(void *s);
+    int               (*pkt_append)(void *s, void *data, size_t size);
+    int               (*send)(void *s);
+    ssize_t           (*recv)(void *s, void **data);
+};
+
+typedef struct smb_srv_info smb_srv_info;
+struct smb_srv_info
+{
+    char                name[16];       // The server name
+    uint16_t            dialect;        // The selected dialect
+    uint16_t            security_mode;  // Security mode
+    uint16_t            uid;            // uid attributed by the server.
+    uint32_t            session_key;    // The session key sent by the server on protocol negotiate
+    uint32_t            caps;           // Server caps replyed during negotiate
+    uint64_t            challenge;      // For challenge response security
+    uint64_t            ts;             // It seems Win7 requires it :-/
+};
+
+/**
+ * @brief An opaque data structure to represent a SMB Session.
+ */
+struct smb_session
+{
+    int                 state;
+    bool                guest;            // Are we logged as guest ?
+
+    // Informations about the smb server we are connected to.
+    smb_srv_info        srv;
+
+
+    ASN1_TYPE           spnego_asn1;
+    smb_buffer          xsec_target;
+
+    smb_creds           creds;
+    smb_transport       transport;
+
+    smb_share           *shares;          // shares->files | Map fd <-> smb_file
+};
+
+typedef struct smb_message smb_message;
+struct smb_message
+{
+    size_t          payload_size; // Size of the allocated payload
+    size_t          cursor;       // Write cursor in the payload
+    smb_packet      *packet;      // Yummy yummy, Fruity fruity !
+};
+
+#endif

+ 2 - 2
src/smb_utils.c

@@ -30,8 +30,8 @@
 # include <langinfo.h>
 #endif
 
-#include "bdsm/debug.h"
-#include "bdsm/smb_utils.h"
+#include "bdsm_debug.h"
+#include "smb_utils.h"
 
 static const char *current_encoding()
 {

+ 2 - 2
include/bdsm/smb_utils.h

@@ -22,8 +22,8 @@
  * @brief SMB helper functions
  */
 
-#ifndef __BDSM_SMB_UTILS_H_
-#define __BDSM_SMB_UTILS_H_
+#ifndef _SMB_UTILS_H_
+#define _SMB_UTILS_H_
 
 #include <stdint.h>