Bladeren bron

Add credentials field in session object

Julien 'Lta' BALLET 11 jaren geleden
bovenliggende
commit
7d572239d0
3 gewijzigde bestanden met toevoegingen van 65 en 3 verwijderingen
  1. 12 0
      include/bdsm/smb_session.h
  2. 13 3
      include/bdsm/smb_types.h
  3. 40 0
      src/smb_session.c

+ 12 - 0
include/bdsm/smb_session.h

@@ -86,6 +86,18 @@ void            smb_session_destroy(smb_session *s);
  */
 int             smb_session_state(smb_session *s);
 
+/**
+ * @brief Set the credentials for this session.
+ * @details Any of the params except s can be NULL.
+ *
+ * @param s The session object.
+ * @param domain Domain to authenticate on. Often it's the same as netbios host.
+ * @param login The user to login as.
+ * @param password the user's password.
+ */
+void            smb_session_set_creds(smb_session *s, const char *domain,
+                                      const char *login, const char *password)
+#define SMB_CREDS_MAXLEN 128
 
 
 // ---------------------------------

+ 13 - 3
include/bdsm/smb_types.h

@@ -94,6 +94,14 @@ typedef struct smb_transport_s {
   ssize_t           (*recv)(void *s, void **data);
 }                   smb_transport;
 
+// An structure to store user credentials;
+// login:password@domain (also DOMAIN\login)
+typedef struct {
+    const char *    domain;
+    const char *    login;
+    const char *    password;
+} smb_creds;
+
 /**
  * @brief An opaque data structure to represent a SMB Session.
  */
@@ -113,18 +121,20 @@ typedef struct
     uint64_t            challenge;      // For challenge response security
     uint64_t            ts;             // It seems Win7 requires it :-/
   }                   srv;
+
   struct {
     void                *init;
     size_t              init_sz;
     ASN1_TYPE           asn1_def;
-    //uint32_t            flags;          // NTLMSSP negotiation flags
   }                   spnego;           // eXtended SECurity negociation data
+
   struct {
     uint32_t            flags;
-    void                *tgt_info;
-    size_t              tgt_info_sz;
+    void                *tgt_info;      // Target info buffer, given by server.
+    size_t              tgt_info_sz;    // The size of the buffer
   }                   xsec;
 
+  smb_creds           creds;
   smb_transport       transport;
   struct smb_share_s  *shares;          // shares->files | Map fd <-> smb_file
 }                   smb_session;

+ 40 - 0
src/smb_session.c

@@ -44,6 +44,10 @@ smb_session   *smb_session_new()
   s->transport.session  = NULL;
   s->shares             = NULL;
 
+  s->creds.domain       = NULL;
+  s->creds.login        = NULL;
+  s->creds.password     = NULL;
+
   return (s);
 }
 
@@ -64,6 +68,15 @@ void            smb_session_destroy(smb_session *s)
       asn1_delete_structure(&s->spnego.asn1_def);
     if (s->xsec.tgt_info != NULL)
       free(s->xsec.tgt_info);
+
+    // Free stored credentials.
+    if (s->creds.domain != NULL)
+      free(s->creds.domain);
+    if (s->creds.login != NULL)
+      free(s->creds.login);
+    if (s->creds.password != NULL)
+      free(s->creds.password);
+
     free(s);
   }
 }
@@ -76,6 +89,33 @@ int             smb_session_state(smb_session *s)
     return (SMB_STATE_ERROR);
 }
 
+void            smb_session_set_creds(smb_session *s, const char *domain,
+                                      const char *login, const char *password)
+{
+  assert(s != NULL);
+
+  if (domain != NULL)
+  {
+    if (s->creds.domain != NULL)
+      free(s->creds.domain);
+    s->creds.domain = strndup(domain, SMB_CREDS_MAXLEN);
+  }
+
+  if (login != NULL)
+  {
+    if (s->creds.login != NULL)
+      free(s->creds.login);
+    s->creds.login = strndup(login, SMB_CREDS_MAXLEN);
+  }
+
+  if (password != NULL)
+  {
+    if (s->creds.password != NULL)
+      free(s->creds.password);
+    s->creds.password = strndup(password, SMB_CREDS_MAXLEN);
+  }
+}
+
 int             smb_session_connect(smb_session *s, const char *name,
                                     uint32_t ip, int transport)
 {