samba.html 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. <style>
  2. .editButton{
  3. color: #83b3d2;
  4. cursor: pointer;
  5. float: right;
  6. }
  7. </style>
  8. <!-- <h3>Enable Samba Service</h3>
  9. <p>Change current systemctl state of smbd (start / stop).</p>
  10. <div class="ui toggle checkbox">
  11. <input type="checkbox" id="enablesmbd">
  12. <label>Enable smbd (Samba Sharing Service)</label>
  13. </div>
  14. -->
  15. <div class="ui divider"></div>
  16. <h3><i class="ui green share alternate icon"></i> Samba Share Lists</h3>
  17. <p>A list of SMB shares currently written into smb.conf</p>
  18. <div style="width: 100%; overflow-y: auto;">
  19. <div id="sharelist">
  20. </div>
  21. <br>
  22. </div>
  23. <!-- Create new Samba Share -->
  24. <h4><i class="ui green circle add icon"></i> Add Samba Share</h4>
  25. <p>Create a new SMB share folder from local disk</p>
  26. <form class="ui form" id="shareForm">
  27. <div class="field">
  28. <label for="shareName">Share Name</label>
  29. <input type="text" id="shareName" placeholder="Share Name">
  30. </div>
  31. <div class="field">
  32. <label for="sharePath">Share Path</label>
  33. <input type="text" id="sharePath" placeholder="/home/user/myshare">
  34. <small><i class="ui yellow exclamation triangle icon"></i> The folder path must be an absolute full path exists on your local disk. e.g. /home/user/myshare/ </small>
  35. </div>
  36. <div class="field">
  37. <label for="validUsers">Valid Users</label>
  38. <select multiple="" class="ui search dropdown" id="validUsers">
  39. </select>
  40. <button onclick="event.preventDefault(); initSambaUserList();" class="ui mini basic button" style="margin-top: 0.4em;"><i class="ui green refresh icon"></i> Refresh User List</button>
  41. </div>
  42. <div class="field">
  43. <div class="ui checkbox">
  44. <input type="checkbox" id="readOnly">
  45. <label for="readOnly">Read Only<br>
  46. <small>Set all files in this share to READ ONLY mode</small></label>
  47. </div>
  48. </div>
  49. <div class="field">
  50. <div class="ui checkbox">
  51. <input type="checkbox" id="browseable" checked>
  52. <label for="browseable">Browseable <br>
  53. <small>Make this share discoverable</small></label>
  54. </div>
  55. </div>
  56. <div class="field">
  57. <div class="ui checkbox">
  58. <input type="checkbox" id="allowGuest">
  59. <label for="allowGuest">Allow Guest<br>
  60. <small>Enable guest account on this share</small></label>
  61. </div>
  62. </div>
  63. <button type="button" class="ui small basic button" onclick="newSambaShare(); event.preventDefault();"><i class="ui green circle add icon"></i> Create Share</button>
  64. </form>
  65. <!-- Create new Samba user -->
  66. <div class="ui divider"></div>
  67. <h3><i class="ui green user circle icon"></i> Samba Users List</h3>
  68. <p>Current list of users registered in Samba database</p>
  69. <div id="userTableContainer"></div>
  70. <h4><i class="ui green user plus icon"></i> Add Samba User</h4>
  71. <p>Create Samba user for sharing<br>
  72. <small>Samba user is not ArozOS user. Creating a Samba user will also create a unix user with login function disabled</small></p>
  73. <div class="ui container">
  74. <form class="ui form" id="userForm">
  75. <div class="field">
  76. <label for="username">Username</label>
  77. <input type="text" id="smbuser_username" placeholder="Enter username" required>
  78. </div>
  79. <div class="field">
  80. <label for="password">Password</label>
  81. <input type="password" id="smbuser_password" placeholder="Enter password" required>
  82. </div>
  83. <div class="field">
  84. <label for="confirmPassword">Confirm Password</label>
  85. <input type="password" id="smbuser_confirmPassword" placeholder="Confirm password" required>
  86. </div>
  87. <button type="button" class="ui basic button" onclick="createNewSambaUser()"><i class="ui checkmark green icon"></i> Confirm</button>
  88. </form>
  89. </div>
  90. <script>
  91. $("#validUsers").dropdown();
  92. $("#shareForm").find("checkbox").checkbox();
  93. //Load all the users in the samba database
  94. function initSambaUserList(){
  95. $.get("../../system/storage/samba/listUsers", function(data){
  96. if (data.error == undefined){
  97. $("#validUsers").html("");
  98. renderUserTable(data);
  99. if (data.length == 0){
  100. return;
  101. }
  102. data.forEach(function(userinfo){
  103. $("#validUsers").append(`<option value="${userinfo.UnixUsername}">${userinfo.UnixUsername}</option>`);
  104. })
  105. }
  106. });
  107. }
  108. initSambaUserList();
  109. //List the current shares in smb.conf
  110. function initShareListTable(){
  111. $.get("../../system/storage/samba/list", function(data){
  112. if (data.error){
  113. msgbox(data.error, false);
  114. }else{
  115. data.sort(function(a, b) {
  116. if (a.Name < b.Name) return -1;
  117. if (a.Name > b.Name) return 1;
  118. return 0;
  119. });
  120. generateTable(data);
  121. }
  122. });
  123. }
  124. initShareListTable();
  125. //Load current smbd state
  126. function initSmbdState(){
  127. $.get("../../system/storage/samba/status", function(data){
  128. if (data.error != undefined){
  129. //Error when loading smbd status
  130. msgbox(data.error, false);
  131. $("#enablesmbd").parent().addClass('disabled');
  132. }else{
  133. if (data == true){
  134. $("#enablesmbd").parent().checkbox("set checked");
  135. }else{
  136. $("#enablesmbd").parent().checkbox("set unchecked");
  137. }
  138. //Bind checkbox event
  139. $("#enablesmbd").on("change", function(data){
  140. let isChecked = $("#enablesmbd")[0].checked;
  141. $.ajax({
  142. url: "../../system/storage/samba/status",
  143. data: {
  144. set: isChecked?"enable":"disable"
  145. },
  146. method: "POST",
  147. success: function(data){
  148. if (data.error != undefined){
  149. msgbox(data.error, false);
  150. $("#enablesmbd").off("change");
  151. initSmbdState();
  152. }else{
  153. msgbox(`SMB Sharing ${isChecked?"enabled":"disabled"}`);
  154. }
  155. }
  156. })
  157. })
  158. }
  159. })
  160. }
  161. initSmbdState();
  162. function generateTable(data) {
  163. // Start the table
  164. let table = `
  165. <table class="ui basic celled unstackable table">
  166. <thead>
  167. <tr>
  168. <th style="min-width: 100px;"><i class="ui yellow folder icon"></i> Name</th>
  169. <th style="min-width: 100px;"><i class="ui grey hdd icon"></i> Path</th>
  170. <th style="min-width: 100px;">Valid Users</th>
  171. <th>Read Only</th>
  172. <th>Browseable</th>
  173. <th>Guest Ok</th>
  174. <th></th>
  175. </tr>
  176. </thead>
  177. <tbody>
  178. `;
  179. // Populate the table rows
  180. data.forEach(item => {
  181. let userListHex = encodeURIComponent(JSON.stringify([]));
  182. if (item.ValidUsers != null){
  183. userListHex = encodeURIComponent(JSON.stringify(item.ValidUsers));
  184. }
  185. table += `
  186. <tr>
  187. <td>${item.Name}</td>
  188. <td>${item.Path} <span class="editButton" onclick="editSharePath('${item.Name}', '${item.Path}');"><i class="edit icon"></i></span></td>
  189. <td>${(item.ValidUsers!=null)?item.ValidUsers.join(", "):'[unix user]'} ${(item.ValidUsers!=null)?`<span class="shareuserEditBtn editButton" onclick="editSambaUser(this, '${item.Name}');" users="${userListHex}"><i class="edit icon"></i></span>`:""}</td>
  190. <td>${item.ReadOnly?'<i class="ui green check icon"></i>':'<i class="ui red times icon"></i>'}</td>
  191. <td>${item.Browseable?'<i class="ui green check icon"></i>':'<i class="ui red times icon"></i>'}</td>
  192. <td>${item.GuestOk?'<i class="ui green check icon"></i>':'<i class="ui red times icon"></i>'}</td>
  193. <td><button title="Remove Share" class="ui basic circular tiny red circular icon button" onclick="deleteSMBShare('${item.Name}');"><i class="ui trash icon"></i></button></td>
  194. </tr>
  195. `;
  196. });
  197. // Close the table
  198. table += `
  199. </tbody>
  200. </table>
  201. `;
  202. if (data.length == 0){
  203. table = `<div class="ui basic message"><i class="ui green check circle icon"></i> No Samba share folder found in smb.conf</div>`
  204. }
  205. // Insert the table into the div
  206. $("#sharelist").html(table);
  207. }
  208. //Create a new samba share
  209. function newSambaShare(){
  210. let shareName = $("#shareName").val().trim();
  211. let sharePath = $("#sharePath").val().trim();
  212. let allowedUsers = $("#validUsers").dropdown("get value");
  213. let isReadOnly = $("#readOnly")[0].checked;
  214. let isBrowseable = $("#browseable")[0].checked;
  215. let allowGuest = $("#allowGuest")[0].checked;
  216. if (shareName == ""){
  217. $("#shareName").parent().addClass("error");
  218. msgbox("Share name cannot be empty", false);
  219. return;
  220. }else{
  221. $("#shareName").parent().removeClass("error");
  222. }
  223. if (sharePath == ""){
  224. $("#sharePath").parent().addClass("error");
  225. msgbox("Share path cannot be empty", false);
  226. return;
  227. }else{
  228. $("#sharePath").parent().removeClass("error");
  229. }
  230. if (allowedUsers.length == 0){
  231. msgbox("At least one user is required to create share");
  232. return;
  233. }
  234. $.ajax({
  235. url: "../../system/storage/samba/add",
  236. method: "POST",
  237. data: {
  238. "name":shareName,
  239. "path": sharePath,
  240. "users": JSON.stringify(allowedUsers),
  241. "readonly":isReadOnly,
  242. "browseable": isBrowseable,
  243. "guestok":allowGuest
  244. },
  245. success: function(data){
  246. if (data.error != undefined){
  247. msgbox(data.error, false, 6000);
  248. }else{
  249. msgbox("New Samba share created");
  250. }
  251. initShareListTable();
  252. }
  253. })
  254. }
  255. //Delete the given smb share name
  256. function deleteSMBShare(smbShareName){
  257. if (confirm("Confirm remove share " + smbShareName + " ?")){
  258. $.ajax({
  259. url: "../../system/storage/samba/remove",
  260. method: "POST",
  261. data:{
  262. "name": smbShareName,
  263. },
  264. success: function(data){
  265. if (data.error != undefined){
  266. msgbox(data.error, false);
  267. }else{
  268. msgbox("Samba share removed");
  269. }
  270. initShareListTable();
  271. //Reload user smb as well if user is admin and have their shares removed
  272. if (typeof(initSMBActivationStatusOnThisUser) != 'undefined'){
  273. initSMBActivationStatusOnThisUser();
  274. }
  275. }
  276. })
  277. }
  278. }
  279. //Render current list of users in samba
  280. function renderUserTable(data) {
  281. // Start the table
  282. let table = `
  283. <table class="ui celled basic small table">
  284. <thead>
  285. <tr>
  286. <th>Unix Username</th>
  287. <th>Domain</th>
  288. <th>ArozOS User</th>
  289. <th>Remove</th>
  290. </tr>
  291. </thead>
  292. <tbody>
  293. `;
  294. // Populate the table rows
  295. data.forEach(item => {
  296. table += `
  297. <tr>
  298. <td><img class="ui avatar image" src="/system/users/profilepic?user=${item.UnixUsername}"> ${item.UnixUsername}</td>
  299. <td>${item.Domain}</td>
  300. <td>${item.IsArozOSUser?"<i class='ui green check icon'></i>":"<i class='ui red times icon'></i>"}</td>
  301. <td><button class="ui basic small red button" onclick="removeSambaUser('${item.UnixUsername}');"><i class="ui red trash icon"></i> Remove Samba User</button></td>
  302. </tr>
  303. `;
  304. });
  305. if (data.length == 0){
  306. table += `<tr><td colspan="3"><i class="ui green circle check icon"></i> No registered users in Samba database</td></tr>`;
  307. }
  308. // Close the table
  309. table += `
  310. </tbody>
  311. </table>
  312. `;
  313. // Insert the table into the div
  314. $('#userTableContainer').html(table);
  315. }
  316. //Create a new samba user
  317. function createNewSambaUser(){
  318. // Get values from the form
  319. const username = $('#smbuser_username').val();
  320. const password = $('#smbuser_password').val();
  321. const confirmPassword = $('#smbuser_confirmPassword').val();
  322. // Check if passwords match
  323. if (password !== confirmPassword) {
  324. msgbox("Confirm password does not match!", false);
  325. }
  326. $.ajax({
  327. url: "/system/storage/samba/addUser",
  328. method: "POST",
  329. data: {
  330. "username": username,
  331. "password": password
  332. },
  333. success: function(data){
  334. if (data.error != undefined){
  335. msgbox(data.error, false, 5000);
  336. }else{
  337. msgbox("New Samba user created");
  338. $("#smbuser_username").val("");
  339. $("#smbuser_password").val("");
  340. $("#smbuser_confirmPassword").val("");
  341. }
  342. //Update the samba user list in share
  343. initSambaUserList();
  344. }
  345. })
  346. }
  347. //Remove a samba user given the username
  348. function removeSambaUser(targetUsername){
  349. if (confirm("Confirm remove samba user \"" + targetUsername + "\" ?")){
  350. $.ajax({
  351. url: "/system/storage/samba/delUser",
  352. method: "POST",
  353. data: {
  354. "username": targetUsername
  355. },
  356. success: function(data){
  357. if (data.error != undefined){
  358. msgbox(data.error, false, 5000);
  359. }else{
  360. msgbox("Samba user removed");
  361. }
  362. //Update the samba user list in share
  363. initSambaUserList();
  364. }
  365. });
  366. }
  367. }
  368. //Inline edit for samba users
  369. function editSambaUser(targetDom, shareName){
  370. let originalUserList = JSON.parse(decodeURIComponent($(targetDom).attr("users")));
  371. let fieldElement = $(targetDom).parent();
  372. $(fieldElement).html(`<i class="loading spinner icon"></i> Loading User List`);
  373. //Overwrite the DOM element with multi-selection dropdown and save button
  374. $.get("../../system/storage/samba/listUsers", function(data){
  375. if (data.error == undefined){
  376. $(".shareuserEditBtn").remove();
  377. //Append user selector with default value selected
  378. $(fieldElement).html(`<select multiple="" class="ui search dropdown" id="editValidUserList"></select>`);
  379. data.forEach(function(userinfo){
  380. $("#editValidUserList").append(`<option value="${userinfo.UnixUsername}">${userinfo.UnixUsername}</option>`);
  381. });
  382. $("#editValidUserList").dropdown();
  383. $("#editValidUserList").dropdown("set selected", originalUserList);
  384. //Append save and cancel button
  385. $(fieldElement).append(`
  386. <div style="margin-top: 0.6em;">
  387. <button class="ui small basic button" onclick="saveSambaUserEdit('${shareName}');"><i class="ui green save icon"></i> Save</button>
  388. <button class="ui small basic button" onclick="initShareListTable();"><i class="ui grey remove icon"></i> Cancel</button>
  389. </div>
  390. `);
  391. }else{
  392. msgbox("Failed to connect to smbd service", false)
  393. }
  394. });
  395. }
  396. function saveSambaUserEdit(shareName){
  397. //Read selection from #editValidUserList
  398. let allowedUsers = $("#editValidUserList").dropdown("get value");
  399. if (allowedUsers.length == 0){
  400. msgbox("At least one user is required per share", false);
  401. return
  402. }
  403. $.ajax({
  404. url: "/system/storage/samba/updateShareUsers",
  405. method: "POST",
  406. data: {
  407. "name": shareName,
  408. "users": JSON.stringify(allowedUsers)
  409. },
  410. success: function(data){
  411. if (data.error != undefined){
  412. msgbox(data.error, false);
  413. }else{
  414. msgbox("Accessible users updated");
  415. }
  416. //Clear share table
  417. initShareListTable();
  418. }
  419. });
  420. }
  421. function editSharePath(shareName, originalSharePath){
  422. let newpath = prompt("New Share Path", originalSharePath);
  423. if (newpath != null && newpath != "") {
  424. $.ajax({
  425. url: "/system/storage/samba/editPath",
  426. method: "POST",
  427. data: {
  428. "name": shareName,
  429. "path": newpath
  430. },
  431. success: function(data){
  432. if (data.error != undefined){
  433. msgbox(data.error, false);
  434. }else{
  435. msgbox("Share path updated");
  436. }
  437. //Clear share table
  438. initShareListTable();
  439. }
  440. });
  441. }
  442. }
  443. </script>