Monday, March 25, 2024

2024-03-23 - File Server Samba (SMB) Login Script

 I was talking to my instructor, Doug,  about how I want to give access to my file server to various computers around my apartment so roommates can watch things downstairs, my mom can use it at her apartment, and so on. But people often accidentally delete things. I know I do this on occasion. I find every once in a while that files are missing and so either I deleted them or there is something happening in file transfers that I cannot nail down. So I would like a way to write protect and or remove write privileges except for when I decide and I can just make sure that nothing happens that I am not intending. 

    Doug talked about different user accounts, one for my mom and one for me so when we built my file server, I created two accounts, one for me with write privileges and one for her without. And if she wants a change, I can just do it for her since she doesn't know how to do it anyway. She can use the files but copy and paste are sorcery as far as she is concerned. Since I care about what happens to the contents I go through the trouble to put on there, I am fine with just being the one that takes care of all that myself. 

    I talked to Doug about other solutions too but Linux is different from how Windows works, and I don't even remember what those conversations were about except that I was surprised that Linux didn't do this or that thing that I am so used to doing on Windows, or at least knew Windows could do. No need to defend Linux, I am sure Linux has features that Windows does not, I am just totally unaware of what they are. 

    I suppose this is a good time to also mention that unlike Windows, Linux has no Recycle Bin. If you delete a file, there is only one protection to stop this action if you catch yourself in time. There is just a dialogue box asking if you are sure you want to delete the file. In Windows this used to be a thing but since Windows 10 I think, it has been optional and I always turn it off with the peace of mind that it is simply moved to the Recycle Bin and if I realize my mistake later, I can recover it. 

    But Linux has no such protection. There is just that dialogue box. I don't remember this conversation with Doug all that well either when he revealed this shocking news to me, but I asked him why every time I delete a file from Windows, even though I have that "Are you sure" dialogue box disabled, I need to sift through files and delete them quickly because I am sifting through thousands of files manually, so the dialogue box really slows me down and plus I am thinking like, what is going on, this option is turned off on my desktop? 

    Doug informed me that because of the way I was deleting the files, this was actually Linux doing this, and I don't remember what way that was, but this was the protection from Linux. And I was like yeah, but why aren't the files then also moving to my Recycle Bin? And he was like, because this is Linux. 

    And I was like, yeah, I know the file server is running Linux, thanks Doug, now back to why the pictures aren't moving to the Recycle Bin. 

    And he starts to subtly smile at this point every time while sort of jokingly being condescending while explaining again, that because this is Linux, there is no Recycle Bin. 

    Huh? This makes no sense! 

    He says sure it does, you're doing this through a method where Linux's safeguards take effect, and these files are located on Linux, so when you delete something, there will be just that dialogue box and once deleted, they will not be moved to the Recycle Bin. 

    Wait, what? Why not?

    Because this is Linux, and Linux does not have a Recycle Bin?

    What manner of barbarism is this? Linux has no Recycle Bin? Well, what happens if you accidentally delete a file?

    That is why there is the dialogue box. 

    That's it?! That is all the protection you get? You mean I was deleting files because I had a trigger finger and that is why when I went to my Windows Recycle bin, there was nothing there? 

    Yes. 

    Long face, jaw on the floor. Dude, seriously... ... ...

    Okay so I will just continue with the lesson for the Fundamentals students. He starts to turn and does something like explaining how the print command puts the code you just created onto the screen in a shell like this is some incredible magic trick. 

    Wait Doug...

    He ignored me. My mind was blown. To be honest, this conversation didn't actually completely go down this way, it was the beginning of class and people were still arriving. But this is the sort of thing that happens with some regularity. I let the newer students think I am stupid and then somehow they get this idea I actually know a lot. C'mon guys, I just unearthed a major discovery that apparently most of the planet already knows...Linux has no Recycle Bin!

    I am sure I inserted some sort of comment in there somewhere in my conversation with Doug saying, and this is the OS that you insisted we should use for my file server?!

    Yes, Windows Server would have costed a thousand dollars and Linux was free. 

    Yeah, anyway, so that conversation was like six months ago or something like that. But about a month ago, I told him yeah okay, lets create more users. I want a read only user so I can put that on the computer downstairs in my living room where my roommates can do whatever they want except delete files. Hey, wait, is there a way to limit what files they have access to? 

    This was another conversation that we had which ended in my being stunned because there's no way to just have one iteration of files in a place where access is limited. Either I need to just have those files by themselves separated from the others for just that user I guess, or something like that, or I need to have two iterations of the same files, which I don't really want to do. I wonder if I can do file shortcuts? Oh wait, no I think that is also something Doug might have said we cannot do. Because if I could create another directory so to speak, just another file tree right next to the file tree with the actual file and then made shortcuts of all those things in the second tree, then I could partition off those for that user and have read only access and all that crap, and also, not have to have multiple iterations or files in different places. 

    I have to ask Doug. He will probably tell me to just do research. Well, he might just answer in the interest of time. 

    Okay, so a month ago we had the idea that we could create multiple users that access all the same files but one would be read only and one would be normal access. And I told him, the only thing I don't like about this is that it would be tedious to log into one to use it, log out and then in again to another to change something, even one small thing, log out again, log back into the other just to use the file without the risk of accidentally deleting it and so on. You could see how this might get tedious at the speed of light. 

    Doug was like, well, there is a way to make that process simpler, it just involves a solution that you might not like, but knowing you, you may be fine with it. We could create a script that logs out of one and into the other, and another script that logs out of that one again and into the first one again. So one to go from 1 to 2, and then one to go from 2 back to 1. That way I get write protections but it's not tedious to the nth degree. The catch is that you have to insert the passwords into the scripts so that it works if you don't want to have to retype the password every few seconds. Done, lets do it Doug! I just have to make sure that no one ever finds these files. That's all! 

    We went through a few iterations of trying to get these scripts to work. We got them to a strange point where they seemed to work if we copied the scripts into the CLI directly, but if we ran the same exact commands as a script, they absolutely would not work. 

    We inserted a delay command for a few seconds in the beginning of the script because Doug thought that perhaps it needed time to log out of the server and wasn't getting enough of a buffer between that and logging into it with another user. 

    Then we tried pausing at the end of the script, and then also adding the exit command. Neither of us remembers why we tried these things. I personally recall that we tried like twenty different things, working on this after class had ended for like a half hour to forty five minuets because Doug just needed to know why this wasn't working and finally ran out of time to solve it. 

    We pasted it in several times and we can't remember what was going on here because it only worked when we pasted it in. It worked that way for both scripts but if you ran the scripts, neither of them worked. 

    Now a few weeks later, I had a scenario at home where I actually needed to use the scripts to log in and out of SAMBA and since pasting the scripts into the Shell worked fine but not running the script, I tried pasting. But now, pasting didn't work either. What is going on. I brought it back in to Doug to see if we could resolve the new added problem. This strikes me as strange because all of the sudden, the actual problem stopping the scripts from working revealed itself but not the reason pasting the script into the Shell was working before and not now. 

    So I grabbed Doug at the end of class to see if he could resolve this issue because I couldn't figure it out. I am not proficient at writing scripts in the first place, let alone writing any code. So this was completely beyond me why this wasn't working. And I was explaining to Doug that pasting was working before when we were trying to troubleshoot the two script files and then that stopped too when I got home so I opened both scripts to edit them in notepad and opened a Shell, whichever one I am not sure, CMD Admin, PowerShell (PS), no idea what I had opened each of these different times, but according to Doug this doesn't matter because "net use" is a CMD command, and will run in PS but because it is a CMD command and not a PS command, it's sort of like PS will use CMD to run the command. So it follows the CMD rules so-to-speak. 

    We pasted the two scripts into the Shell and all of the sudden, Doug realized something he didn't realize last time. The emoji that Jack in class told me like six or so months ago to include in the file path for my server, the skull and crossbones emoji, which was added initially as a joke, I was trying to be cool and said yeah sure lets keep it. And it has caused problems ever since because anytime I need to type it in I have to go online and fine the emoji and depending on what OS you're on, emojis display different, so my iPhone would display the same one a slightly different way that confused me and caused me to think it was the wrong emoji for a while six months ago when connecting my phone to the server, and then now, with the log-in and log-out scripts, turns out CMD does not recognize emoji characters because it runs using Ascii, while PowerShell runs on Unicode. 

    Just for a refresher or whatever, Ascii is an older standard and only has like 128 characters or something. But Unicode is newer, and there are many types too, and it has new characters added to it all the time. It has hundreds of thousands. 

    In talking about this, Zack and Doug started debating how many characters there were. I swear we're not geeks!

    The finalized versions of the two scripts are as follows, for the script to delete the READONLY log-in and log into the regular user with all privileges:


"net use /delete S:

net use S: "\\192.168.XXX.XXX\KeepOut" /USER:biff (password here)"


Then the script for logging out of the regular user with privileges and back into the READONLY user:


"@echo off

net use /delete S:

net use S: \\192.168.XXX.XXX\KeepOut /USER:readonly (password here)

exit"


    We had forgotten to delete the exit command at the end of the second script before I wrote this blog. Both scripts now run great! 

This has been Truncat3d 00000000111100010100110______________end of line

No comments:

Post a Comment

2025-07-10 - Active Directory 5.0 - Group Policy Foundations: Understanding Domain Admins and User Accounts / Setting up Remote Access

  Why You Use TESTLAB\Administrator Across Multiple Machines — And Why You Need Separate Domain Users When you join a workstation to an Act...