]> git.ipfire.org Git - thirdparty/squid.git/blobdiff - lib/smblib/smblib.c
Source Format Enforcement (#763)
[thirdparty/squid.git] / lib / smblib / smblib.c
index d32406a5e9ff5d36f41711e0e28061a4f9543d3b..8ec836a12ab52a8c47e21c4ab71adedc7450d879 100644 (file)
@@ -1,4 +1,10 @@
-#include "squid.h"
+/*
+ * Copyright (C) 1996-2021 The Squid Software Foundation and contributors
+ *
+ * Squid software is distributed under GPLv2+ license and includes
+ * contributions from numerous individuals and organizations.
+ * Please see the COPYING and CONTRIBUTORS files for details.
+ */
 
 /* UNIX SMBlib NetBIOS implementation
 
@@ -6,7 +12,6 @@
    SMBlib Routines
 
    Copyright (C) Richard Sharpe 1996
-
 */
 
 /*
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
 
+#include "squid.h"
+
 int SMBlib_errno;
 int SMBlib_SMB_Error;
 #define SMBLIB_ERRNO
 
-#include "smblib/smblib.h"
-#include "smblib/smblib-priv.h"
 #include "rfcnb/rfcnb.h"
+#include "smblib/smblib-priv.h"
+#include "smblib/smblib.h"
 
 #include <signal.h>
 #if HAVE_STRING_H
@@ -55,7 +62,6 @@ const char *SMB_Prots[] = {"PC NETWORK PROGRAM 1.0",
                            NULL
                           };
 
-
 /* Initialize the SMBlib package     */
 
 int SMB_Init()
@@ -93,7 +99,7 @@ SMB_Handle_Type SMB_Create_Con_Handle()
 /* or anything else ...                                                  */
 
 SMB_Handle_Type SMB_Connect_Server(SMB_Handle_Type Con_Handle,
-                                   char *server, const char *NTdomain)
+                                   const char *server, const char *NTdomain)
 
 {
     SMB_Handle_Type con;
@@ -108,7 +114,6 @@ SMB_Handle_Type SMB_Connect_Server(SMB_Handle_Type Con_Handle,
 
         if ((con = (struct SMB_Connect_Def *)malloc(sizeof(struct SMB_Connect_Def))) == NULL) {
 
-
             SMBlib_errno = SMBlibE_NoSpace;
             return NULL;
         }
@@ -122,8 +127,10 @@ SMB_Handle_Type SMB_Connect_Server(SMB_Handle_Type Con_Handle,
     strcpy(con -> password, "");
     strcpy(con -> sock_options, "");
     strcpy(con -> address, "");
-    strcpy(con -> desthost, server);
-    strcpy(con -> PDomain, NTdomain);
+    strncpy(con -> desthost, server, sizeof(con->desthost));
+    con->desthost[sizeof(con->desthost) - 1] = '\0';
+    strncpy(con -> PDomain, NTdomain, sizeof(con->PDomain));
+    con->PDomain[sizeof(con->PDomain) - 1] = '\0';
     strcpy(con -> OSName, SMBLIB_DEFAULT_OSNAME);
     strcpy(con -> LMType, SMBLIB_DEFAULT_LMTYPE);
     con -> first_tree = con -> last_tree = NULL;
@@ -142,17 +149,17 @@ SMB_Handle_Type SMB_Connect_Server(SMB_Handle_Type Con_Handle,
     /* Now connect to the remote end, but first upper case the name of the
        service we are going to call, sine some servers want it in uppercase */
 
-    for (i=0; i < strlen(server); i++)
-        called[i] = toupper(server[i]);
+    for (i=0; i < strlen(con -> desthost); i++)
+        called[i] = xtoupper(con -> desthost[i]);
 
-    called[strlen(server)] = 0;    /* Make it a string */
+    called[strlen(con -> desthost)] = 0;    /* Make it a string */
 
     for (i=0; i < strlen(con -> myname); i++)
-        calling[i] = toupper(con -> myname[i]);
+        calling[i] = xtoupper(con -> myname[i]);
 
     calling[strlen(con -> myname)] = 0;    /* Make it a string */
 
-    if (strcmp(con -> address, "") == 0)
+    if (strlen(con -> address) == 0)
         address = con -> desthost;
     else
         address = con -> address;
@@ -187,7 +194,6 @@ const char *SMB_Prots_Restrict[] = {"PC NETWORK PROGRAM 1.0",
                                     NULL
                                    };
 
-
 SMB_Handle_Type SMB_Connect(SMB_Handle_Type Con_Handle,
                             SMB_Tree_Handle *tree,
                             char *service,
@@ -216,9 +222,12 @@ SMB_Handle_Type SMB_Connect(SMB_Handle_Type Con_Handle,
 
     /* Init some things ... */
 
-    strcpy(con -> service, service);
-    strcpy(con -> username, username);
-    strcpy(con -> password, password);
+    strncpy(con -> service, service, sizeof(con -> service));
+    con -> service[sizeof(con -> service) - 1] = '\0';
+    strncpy(con -> username, username, sizeof(con -> username));
+    con -> username[sizeof(con -> username) - 1] = '\0';
+    strncpy(con -> password, password, sizeof(con -> password));
+    con -> password[sizeof(con -> password) - 1] = '\0';
     strcpy(con -> sock_options, "");
     strcpy(con -> address, "");
     strcpy(con -> PDomain, SMBLIB_DEFAULT_DOMAIN);
@@ -239,24 +248,34 @@ SMB_Handle_Type SMB_Connect(SMB_Handle_Type Con_Handle,
 
     /* Now figure out the host portion of the service */
 
-    strcpy(temp, service);
+    strncpy(temp, service, sizeof(temp));
+    temp[sizeof(temp) - 1] = '\0';
     host = strtok(temp, "/\\");     /* Separate host name portion */
-    strcpy(con -> desthost, host);
+    if (!host) {
+        if (Con_Handle == NULL) {
+            free(con);
+            Con_Handle = NULL;
+        }
+        SMBlib_errno = -SMBlibE_CallFailed;
+        return NULL;
+    }
+    strncpy(con->desthost, host, sizeof(con->desthost));
+    con->desthost[sizeof(con->desthost)-1]='\0';
 
     /* Now connect to the remote end, but first upper case the name of the
        service we are going to call, sine some servers want it in uppercase */
 
-    for (i=0; i < strlen(host); i++)
-        called[i] = toupper(host[i]);
+    for (i=0; i < strlen(con -> desthost); i++)
+        called[i] = xtoupper(con -> desthost[i]);
 
-    called[strlen(host)] = 0;    /* Make it a string */
+    called[strlen(con -> desthost)] = 0;    /* Make it a string */
 
     for (i=0; i < strlen(con -> myname); i++)
-        calling[i] = toupper(con -> myname[i]);
+        calling[i] = xtoupper(con -> myname[i]);
 
     calling[strlen(con -> myname)] = 0;    /* Make it a string */
 
-    if (strcmp(con -> address, "") == 0)
+    if (strlen(con -> address) == 0)
         address = con -> desthost;
     else
         address = con -> address;
@@ -283,9 +302,10 @@ SMB_Handle_Type SMB_Connect(SMB_Handle_Type Con_Handle,
 
     if (SMB_Negotiate(con, SMB_Prots_Restrict) < 0) {
 
-        /* Hmmm what should we do here ... We have a connection, but could not
-           negotiate ...                                                      */
-
+        if (Con_Handle == NULL) {
+            free(con);
+        }
+        SMBlib_errno = -SMBlibE_NegNoProt;
         return NULL;
 
     }
@@ -294,6 +314,10 @@ SMB_Handle_Type SMB_Connect(SMB_Handle_Type Con_Handle,
 
     if ((*tree = SMB_TreeConnect(con, NULL, service, password, "A:")) == NULL) {
 
+        if (Con_Handle == NULL) {
+            free(con);
+        }
+        SMBlib_errno = -SMBlibE_BAD;
         return NULL;
 
     }
@@ -328,7 +352,8 @@ int SMB_Logon_Server(SMB_Handle_Type Con_Handle, char *UserName,
         pass_len = 24;
         memcpy(pword, PassWord, 24);
     } else {
-        strcpy(pword, PassWord);
+        strncpy(pword, PassWord, sizeof(pword));
+        pword[sizeof(pword) - 1] = '\0';
 #ifdef PAM_SMB_ENC_PASS
         if (Con_Handle->encrypt_passwords) {
             pass_len = 24;
@@ -394,7 +419,7 @@ int SMB_Logon_Server(SMB_Handle_Type Con_Handle, char *UserName,
 
         p = p + 1;
 
-        if (NtDomain != NULL) {
+        if (NtDomain == NULL) {
             strcpy(p, Con_Handle -> PDomain);
             p = p + strlen(Con_Handle -> PDomain);
         } else {
@@ -547,7 +572,6 @@ int SMB_Logon_Server(SMB_Handle_Type Con_Handle, char *UserName,
 
 }
 
-
 /* Disconnect from the server, and disconnect all tree connects */
 
 int SMB_Discon(SMB_Handle_Type Con_Handle, BOOL KeepHandle)
@@ -564,3 +588,4 @@ int SMB_Discon(SMB_Handle_Type Con_Handle, BOOL KeepHandle)
     return(0);
 
 }
+