浏览代码

Tweak and merge pull request #1 on lu-zero/libdsm from reubenhwk/cleanups.

Julien 'Lta' BALLET 11 年之前
父节点
当前提交
873511fbfb
共有 8 个文件被更改,包括 97 次插入31 次删除
  1. 71 15
      bin/dsm.c
  2. 3 1
      bin/lookup.c
  3. 3 1
      include/bdsm/netbios_ns.h
  4. 3 2
      include/bdsm/netbios_session.h
  5. 2 1
      include/bdsm/smb_session.h
  6. 10 8
      src/netbios_ns.c
  7. 3 2
      src/netbios_session.c
  8. 2 1
      src/smb_session.c

+ 71 - 15
bin/dsm.c

@@ -37,18 +37,73 @@
 #include "bdsm.h"
 #include "bdsm/smb_trans2.h"
 
-#include <openssl/md4.h>
-#include <openssl/md5.h>
+#include <getopt.h>
+
+static int parse_options(int argc, char * argv[])
+{
+  /* *INDENT-OFF* */
+  char usage_str[] = {
+    "  -h, --help         Show this help screen.\n"
+    "  -v, --version      Print the version and quit.\n"
+  };
+  /* *INDENT-ON* */
+
+  struct option long_options[] = {
+    {"help", no_argument, 0, 'h'},
+    {"version", no_argument, 0, 'v'},
+    {0, 0, 0, 0},
+  };
+
+  int c, opt_index = 0;
+
+  char const *pname = ((pname = strrchr(argv[0], '/')) != NULL) ? pname + 1 : argv[0];
+
+  while (0 < (c = getopt_long(argc, argv, "hv", long_options, &opt_index)) ) {
+    switch (c) {
+
+    case 'h':
+      fprintf(stderr, "%s", usage_str);
+      exit(0);
+
+    case 'v':
+      fprintf(stderr, "v%s\n", VERSION);
+      exit(0);
+
+    default:
+      fprintf(stderr, "unknown option, %c, in getopt_long.\n", c);
+      exit(-1);
+    }
+  }
+
+  return optind;
+}
 
 int main(int ac, char **av)
 {
+  const char          *pname, *host, *login, *password, *fname;
   struct sockaddr_in  addr;
   bdsm_context_t      *ctx;
+  int                 argoffset;
+
+  pname     = ((pname = strrchr(av[0], '/')) != NULL) ? pname + 1 : av[0];
+  argoffset = parse_options(ac, av);
+
+  if (argoffset >= ac) {
+    fprintf(stderr, "usage: %s [options] host login password file\n", pname);
+    exit(-1);
+  }
+
+  host      = av[argoffset++];
+  login     = av[argoffset++];
+  password  = av[argoffset++];
+  fname     = av[argoffset++];
 
   ctx = bdsm_context_new();
   assert(ctx);
-  addr.sin_addr.s_addr = netbios_ns_resolve(ctx->ns, av[1], NETBIOS_FILESERVER);
-  printf("%s's IP address is : %s\n", av[1], inet_ntoa(addr.sin_addr));
+  if (0 != netbios_ns_resolve(ctx->ns, host, NETBIOS_FILESERVER, &addr.sin_addr.s_addr)) {
+    exit(-1);
+  }
+  printf("%s's IP address is : %s\n", host, inet_ntoa(addr.sin_addr));
 
   //netbios_ns_discover(ctx->ns);
   //exit(0);
@@ -56,10 +111,10 @@ int main(int ac, char **av)
   // netbios_session_t *session;
   // session = netbios_session_new(addr.sin_addr.s_addr);
   // if (netbios_session_connect(session, "Cerbere"))
-  //   printf("A NetBIOS session with %s has been established\n", av[1]);
+  //   printf("A NetBIOS session with %s has been established\n", host);
   // else
   // {
-  //   printf("Unable to establish a NetBIOS session with %s\n", av[1]);
+  //   printf("Unable to establish a NetBIOS session with %s\n", host);
   //   exit(21);
   // }
 
@@ -67,24 +122,25 @@ int main(int ac, char **av)
 
   smb_session_t *session;
   session = smb_session_new();
-  if (smb_session_connect(session, av[1], addr.sin_addr.s_addr))
+
+  if (smb_session_connect(session, host, addr.sin_addr.s_addr))
   {
-    printf("Successfully connected to %s\n", av[1]);
+    printf("Successfully connected to %s\n", host);
     fprintf(stderr, "Session key is 0x%x\n", session->srv.session_key);
     fprintf(stderr, "Challenge key is 0x%lx\n", session->srv.challenge);
   }
   else
   {
-    printf("Unable to connect to %s\n", av[1]);
+    printf("Unable to connect to %s\n", host);
     exit(42);
   }
 
-  if (smb_authenticate(session, av[1], av[2], av[3]))
+  if (smb_authenticate(session, host, login, password))
   {
     if (session->guest)
       printf("Login FAILED but we were logged in as GUEST \n");
     else
-      printf("Successfully logged in as %s\\%s\n", av[1], av[2]);
+      printf("Successfully logged in as %s\\%s\n", host, login);
   }
   else
   {
@@ -130,7 +186,7 @@ int main(int ac, char **av)
 
   if (!smb_share_list(session, &share_list))
   {
-    fprintf(stderr, "Unable to list share for %s\n", av[1]);
+    fprintf(stderr, "Unable to list share for %s\n", host);
     exit(42);
   }
   else
@@ -149,11 +205,11 @@ int main(int ac, char **av)
   else
     fprintf(stderr, "Unable to list files\n");
 
-  fprintf(stderr, "Query file info for path: %s\n", av[4]);
-  files = smb_stat(session, test, av[4]);
+  fprintf(stderr, "Query file info for path: %s\n", fname);
+  files = smb_stat(session, test, fname);
 
   if (files)
-    printf("File %s is %lu bytes long\n", av[4], files->size);
+    printf("File %s is %lu bytes long\n", fname, files->size);
 
 
   smb_session_destroy(session);

+ 3 - 1
bin/lookup.c

@@ -43,7 +43,9 @@ int main(int ac, char **av)
     exit(1);
   }
 
-  addr.s_addr = netbios_ns_resolve(ctx->ns, av[1], NETBIOS_FILESERVER);
+  if (0 != netbios_ns_resolve(ctx->ns, av[1], NETBIOS_FILESERVER, &addr.s_addr)) {
+    exit(-1);
+  }
   if (!addr.s_addr)
   {
     fprintf(stderr, "Unable to perform name resolution for %s\n", av[1]);

+ 3 - 1
include/bdsm/netbios_ns.h

@@ -107,9 +107,11 @@ void          netbios_ns_destroy(netbios_ns_t *ns);
  * @param name the null-terminated ASCII netbios name to resolve. If it's
  * longer than 15 chars, it'll be truncated.
  * @param type The type of the name to look for. @see netbios_defs.h
+ * @param[out] addr The IP address in network byte order of the machine if found.
  * @return the ipv4 address in network byte-order or 0 if it wasn't successfull.
  */
-uint32_t      netbios_ns_resolve(netbios_ns_t *ns, const char *name, char type);
+int             netbios_ns_resolve(netbios_ns_t *ns, const char *name,
+                                   char type, uint32_t * addr);
 
 /**
  * @brief Try to discover all the Netbios/SMB speaking machine on the LAN.

+ 3 - 2
include/bdsm/netbios_session.h

@@ -58,11 +58,12 @@ typedef struct              netbios_session_s {
 netbios_session_t *netbios_session_new(uint32_t ip_addr);
 void              netbios_session_destroy(netbios_session_t *);
 
-int               netbios_session_connect(netbios_session_t *s, char *name);
+int               netbios_session_connect(netbios_session_t *s,
+                                          const char *name);
 void              netbios_session_packet_init(netbios_session_t *s,
                                               uint8_t opcode);
 int               netbios_session_packet_append(netbios_session_t *s,
-                                                char *data, size_t size);
+                                                const char *data, size_t size);
 int               netbios_session_packet_send(netbios_session_t *s);
 ssize_t           netbios_session_packet_recv(netbios_session_t *s);
 

+ 2 - 1
include/bdsm/smb_session.h

@@ -198,7 +198,8 @@ size_t          smb_session_recv_msg(smb_session_t *s, smb_message_t *msg);
  * @param ip The ip of the machine to connect to (in network byte order)
  * @return 0 in case of error, a value > 0 otherwise.
  */
-int             smb_session_connect(smb_session_t *s, char *name, uint32_t ip);
+int             smb_session_connect(smb_session_t *s, const char *name,
+                                    uint32_t ip);
 
 /**
  * @brief Authenticate on the remote host with the provided credentials

+ 10 - 8
src/netbios_ns.c

@@ -160,7 +160,7 @@ ssize_t           netbios_ns_recv(int sock, void *buf, size_t buf_size,
     return (-1);
 }
 
-uint32_t      netbios_ns_resolve(netbios_ns_t *ns, const char *name, char type)
+int      netbios_ns_resolve(netbios_ns_t *ns, const char *name, char type, uint32_t * addr)
 {
   netbios_ns_entry_t  *cached;
   struct timeval      timeout;
@@ -180,7 +180,7 @@ uint32_t      netbios_ns_resolve(netbios_ns_t *ns, const char *name, char type)
     return (cached->address.s_addr);
 
   if ((encoded_name = netbios_name_encode(name, 0, type)) == NULL)
-    return (0);
+    return (-1);
 
     // Prepare packet
   q = netbios_query_new(34 + 4, 1, NETBIOS_OP_NAME_QUERY);
@@ -211,15 +211,17 @@ uint32_t      netbios_ns_resolve(netbios_ns_t *ns, const char *name, char type)
   recv = netbios_ns_recv(ns->socket, (void *)recv_buffer, 512, &timeout, 0, 0);
 
   if (recv <= 0)
-    goto error;
+    perror("netbios_ns_resolve:");
+  else if (recv == 0)
+    BDSM_dbg("netbios_ns_resolve, received NO reply for '%s' !\n", name);
   else
+  {
     BDSM_dbg("netbios_ns_resolve, received a reply for '%s' !\n", name);
+    *addr = (*(uint32_t *)(recv_buffer + recv - 4));
+    return (1);
+  }
 
-  return (*(uint32_t *)(recv_buffer + recv - 4));
-
-  error:
-    perror("netbios_ns_resolve: ");
-    return (0);
+  return (0);
 }
 
 

+ 3 - 2
src/netbios_session.c

@@ -79,7 +79,8 @@ void              netbios_session_destroy(netbios_session_t *s)
   free(s);
 }
 
-int               netbios_session_connect(netbios_session_t *s, char *name)
+int               netbios_session_connect(netbios_session_t *s,
+                                          const char *name)
 {
   netbios_session_packet_t  *received;
   ssize_t                   recv_size;
@@ -139,7 +140,7 @@ void              netbios_session_packet_init(netbios_session_t *s,
 }
 
 int               netbios_session_packet_append(netbios_session_t *s,
-                                                char *data, size_t size)
+                                                const char *data, size_t size)
 {
   char  *start;
 

+ 2 - 1
src/smb_session.c

@@ -55,7 +55,8 @@ int             smb_session_state(smb_session_t *s)
     return (SMB_STATE_ERROR);
 }
 
-int             smb_session_connect(smb_session_t *s, char *name, uint32_t ip)
+int             smb_session_connect(smb_session_t *s, const char *name,
+                                    uint32_t ip)
 {
   assert(s != NULL && name != NULL);