aroz 1 год назад
Родитель
Сommit
8e37a14b15

+ 19 - 0
mod/fileservers/servers/samba/handlers.go

@@ -109,6 +109,12 @@ func (s *ShareManager) AddSambaShare(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
+	//Check if it is a reserved / protected path
+	if isPathInsideImportantFolders(absoluteSharePath) {
+		utils.SendErrorResponse(w, "system reserved path cannot be shared")
+		return
+	}
+
 	validUsersJSON, err := utils.PostPara(r, "users")
 	if err != nil {
 		utils.SendErrorResponse(w, "no valid user givens")
@@ -124,6 +130,19 @@ func (s *ShareManager) AddSambaShare(w http.ResponseWriter, r *http.Request) {
 	}
 
 	//Check if all the users exists in the host OS
+	for _, validUser := range validUsers {
+		thisUnixUserExists, err := s.SambaUserExists(validUser)
+		if err != nil {
+			utils.SendErrorResponse(w, err.Error())
+			return
+		}
+
+		if !thisUnixUserExists {
+			//This user not exists
+			utils.SendErrorResponse(w, validUser+" is not a valid unix user")
+			return
+		}
+	}
 
 	readOnly, err := utils.PostBool(r, "readonly")
 	if err != nil {

+ 51 - 0
mod/fileservers/servers/samba/helpers.go

@@ -3,6 +3,7 @@ package samba
 import (
 	"fmt"
 	"os/exec"
+	"path/filepath"
 	"strings"
 )
 
@@ -39,3 +40,53 @@ func restartSmbd() error {
 	}
 	return nil
 }
+
+// Check if a samba username exists (unix username only)
+func (s *ShareManager) SambaUserExists(username string) (bool, error) {
+	userInfos, err := s.ListSambaUsersInfo()
+	if err != nil {
+		return false, err
+	}
+
+	for _, userInfo := range userInfos {
+		if userInfo.UnixUsername == username {
+			return true, nil
+		}
+	}
+
+	return false, nil
+}
+
+// List of important folders not to be shared via SMB
+var importantFolders = []string{
+	"/bin",
+	"/boot",
+	"/dev",
+	"/etc",
+	"/lib",
+	"/lib64",
+	"/proc",
+	"/root",
+	"/sbin",
+	"/sys",
+	"/tmp",
+	"/usr",
+	"/var",
+}
+
+// IsPathInsideImportantFolders checks if the given path is inside one of the important folders
+func isPathInsideImportantFolders(path string) bool {
+	// Clean the given path
+	cleanedPath := filepath.Clean(path)
+
+	// Iterate over the important folders
+	for _, folder := range importantFolders {
+		// Clean the important folder path
+		cleanedFolder := filepath.Clean(folder)
+		// Check if the cleaned path is inside the cleaned folder
+		if strings.HasPrefix(cleanedPath, cleanedFolder) {
+			return true
+		}
+	}
+	return false
+}

+ 65 - 12
web/SystemAO/disk/samba.html

@@ -7,9 +7,13 @@
 
 <h3><i class="ui green share alternate icon"></i> Samba Share Lists</h3>
 <p>A list of SMB shares currently written into smb.conf</p>
-<div id="sharelist">
+<div style="width: 100%; overflow-y: auto;">
+    <div id="sharelist">
 
+    </div>
+    <br>
 </div>
+
 <div class="ui divider"></div>
 <h3><i class="ui green circle add icon"></i> Add Samba Share</h3>
 <p>Create a new SMB share folder from local disk</p>
@@ -26,10 +30,6 @@
     <div class="field">
         <label for="validUsers">Valid Users</label>
         <select multiple="" class="ui search dropdown" id="validUsers">
-            <option value="user1">User 1</option>
-            <option value="user2">User 2</option>
-            <option value="user3">User 3</option>
-            <!-- Add options for all users in the system -->
         </select>
     </div>
     <div class="field">
@@ -53,7 +53,7 @@
             <small>Enable guest account on this share</small></label>
         </div>
     </div>
-    <button type="button" class="ui small basic button" onclick="submitForm()"><i class="ui green circle add icon"></i> Create Share</button>
+    <button type="button" class="ui small basic button" onclick="newSambaShare(); event.preventDefault();"><i class="ui green circle add icon"></i> Create Share</button>
 </form>
 </div>
 
@@ -135,13 +135,13 @@
                 <table class="ui basic celled unstackable table">
                     <thead>
                         <tr>
-                            <th><i class="ui yellow folder icon"></i> Name</th>
-                            <th><i class="ui grey hdd icon"></i> Path</th>
-                            <th>Valid Users</th>
+                            <th style="min-width: 100px;"><i class="ui yellow folder icon"></i> Name</th>
+                            <th style="min-width: 100px;"><i class="ui grey hdd icon"></i> Path</th>
+                            <th style="min-width: 100px;">Valid Users</th>
                             <th>Read Only</th>
                             <th>Browseable</th>
                             <th>Guest Ok</th>
-                            <th>Actions</th>
+                            <th></th>
                         </tr>
                     </thead>
                     <tbody>
@@ -172,6 +172,59 @@
             $("#sharelist").html(table);
         }
 
+        //Create a new samba share
+        function newSambaShare(){
+            let shareName = $("#shareName").val().trim();
+            let sharePath = $("#sharePath").val().trim();
+            let allowedUsers = $("#validUsers").dropdown("get value");
+            let isReadOnly = $("#readOnly")[0].checked;
+            let isBrowseable = $("#browseable")[0].checked;
+            let allowGuest = $("#allowGuest")[0].checked;
+
+            if (shareName == ""){
+                $("#shareName").parent().addClass("error");
+                msgbox("Share name cannot be empty", false);
+                return;
+            }else{
+                $("#shareName").parent().removeClass("error");
+            }
+
+            if (sharePath == ""){
+                $("#sharePath").parent().addClass("error");
+                msgbox("Share path cannot be empty", false);
+                return;
+            }else{  
+                $("#sharePath").parent().removeClass("error");
+            }
+
+            if (allowedUsers.length == 0){
+                msgbox("At least one user is required to create share");
+                return;
+            }
+
+
+            $.ajax({
+                url: "../../system/storage/samba/add",
+                method: "POST",
+                data: {
+                    "name":shareName,
+                    "path": sharePath,
+                    "users": JSON.stringify(allowedUsers),
+                    "readonly":isReadOnly,
+                    "browseable": isBrowseable,
+                    "guestok":allowGuest
+                },
+                success: function(data){
+                    if (data.error != undefined){
+                        msgbox(data.error, false, 6000);
+                    }else{
+                        msgbox("New Samba share created");
+                    }
+                    initShareListTable();
+                }
+            })
+        }
+
         //Delete the given smb share name
         function deleteSMBShare(smbShareName){
             if (confirm("Confirm remove share " + smbShareName + " ?")){
@@ -185,9 +238,9 @@
                         if (data.error != undefined){
                             msgbox(data.error, false);
                         }else{
-                            msgbox("SMB share removed");
-                            initShareListTable();
+                            msgbox("Samba share removed");
                         }
+                        initShareListTable();
                     }
                 })
             }