Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

NEED HELP IN LINUX SCRIPTING. NEED TO CREATE A SCRIPT THAT ALLOWS NEW USERS TO B

ID: 3763680 • Letter: N

Question

NEED HELP IN LINUX SCRIPTING. NEED TO CREATE A SCRIPT THAT ALLOWS NEW USERS TO BE ADDED. The script should take no command line arguments, but should read a list of new users to add from a file that is redirected from stdin using the < operator. You must check that you get exactly zero arguments to the script. This input file should contain a series of lines, one for each new user to add. Each line has two fields: the first name and the last name of the new user, separated by a comma. Here is an example file named NEW_USERS JOHN, DOE BOBBY, Smith Janet, Woo This file specifies three new users to be added. There are two tricky parts about this script. First, you have to build the usernames automatically from the first and last names. Second, you have to set an initial password for each new user. Best practice is to create a random password for each user. Both of these tasks require the use of the "tr" command that is used to translate and/or delete characters from a stream of input: 1. The simplest form of the command is: echo "STRING" | tr SET1 SET2 2. This translates every character in STRING by switching any characters in SET1 to the corresponding character in SET2. 3. The most common usage is to translate upper case characters to lower case (or vice versa). To do that: echo "SomeThing" | tr 'A-Z' 'a-z' 4. The tr 'A-Z' 'a-z' says to translate any A to a, any B to b, any C to c, and so on. The output of the above command would thus be "something" (the S changed to lower case s and the T changed to lower case t). All other characters are left unchanged. So, to generate the username you'll need to: 1. Get the first and last names out of the file individually. 2. Translate both to lower case. 3. Use the bash substring syntax we used previously or use cut to get the first letter of the last name. 4. Concatenate the first name and the first letter of the last name. 5. Assume that the username is not in use (you don't need to check that someone else already has that username for now). Recall that you can use the cut command to split each line up into the first and last name fields (by comma separator). Also, you'll need to remember how to use while loops that read lines from stdin, so that you can repeat this processing for each user in the file.

Explanation / Answer

'New User Creation Script

'Option Explicit
Dim WshShell, fso
Set WSHShell = User.CreateObject("User.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
Set WshNetwork = User.CreateObject("User.Network")

DomainName = "domain.com"
OUNamePt1 = "User_Accounts"
OUNamePt2 = InputBox("Enter the user")
If OUNamePt2 = "" Then
   Wscript.quit
End If
OUNamePt3="UK"
OUNamePt4="AcmeGroup"
DefaultPassword = "DEFAULTPASSWORD"
Set dom = GetObject("LDAP://" & DomainName)
InputPrompt1 = "Domain = "&DomainName&vbCrLf&"Container = "& OUNamePt2&vbCrLf&vbCrLf&"Enter User's Name"
InputPrompt2 = "Domain = "&DomainName&vbCrLf&"Container = "& OUNamePt2&vbCrLf&vbCrLf&"Enter User's First Name"
InputPrompt3 = "Domain = "&DomainName&vbCrLf&"Container = "& OUNamePt2&vbCrLf&vbCrLf&"Enter User's Surname"
UserName = InputBox(InputPrompt1, "User Initials")
If UserName = "" Then
   Wscript.quit
End If
FirstName = InputBox(InputPrompt2, "First Name")
If FirstName = "" Then
   Wscript.quit
End If
Surname = InputBox(InputPrompt3, "Surname")
If Surname = "" Then
   Wscript.quit
End If
Set usr = dom.Create("user", "CN=" & Surname & " " & FirstName & ",OU=" & OUNamePt1 & ",OU=" & OUNamePt2 & ",OU=" & OUNamePt3 & ",OU=" & OUNamePt4)
Set ProfileServer = fso.GetFolder("\serverhomedrive$")

'Create User
usr.put "samAccountName", LCase(UserName)
usr.put "userPrincipalName", FirstName & "." & Surname & "@" & DomainName
usr.put "givenName", FirstName
usr.put "sn", Surname
usr.put "displayName", Surname & " " & FirstName
usr.put "homeDirectory", "\serverhomedrive$" & LCase(UserName)
usr.put "homeDrive", "H:"
usr.put "scriptPath", "UkAcmeLogon.bat"
usr.put "displayNamePrintable", FirstName & " " & Surname
usr.put "mailNickname", FirstName & "." & Surname
usr.put "name", Surname & " " & FirstName
usr.setinfo
usr.setpassword DefaultPassword
usr.accountdisabled = False
usr.setinfo

'Create User's Mailbox
Dim oIADSUser
Dim MStore
Set oIADSUser = GetObject("LDAP://CN=" & Surname & " " & FirstName & ",OU=User_Accounts, OU=" & OUNamePt2 & ",OU=UK,OU=AcmeGroup,DC=domain,DC=com")

MStore = "MailboxStoreName"


oIADSUser.CreateMailbox "LDAP://CN=" & MStore & ",CN=UK Storage Group,CN=InformationStore,CN=EXCHANGE SERVER,CN=Servers,CN=AcmeUK,CN=Administrative Groups,CN=acmegroup,CN=Microsoft Exchange,CN=Services,CN=Configuration,DC=domain,DC=com"
oIADSUser.SetInfo

'Add member to groups
Const ADS_PROPERTY_APPEND = 3

Set objGroup = GetObject("LDAP://CN=Ironport Users,OU=ACL Groups,OU=Groups,OU=Lei,OU=UK,OU=AcmeGroup,DC=domain,DC=com")
objGroup.PutEx ADS_PROPERTY_APPEND, "member", Array("CN=" & Surname & " " & FirstName & ",OU=User_Accounts, OU=" & OUNamePt2 & ",OU=UK,OU=AcmeGroup,DC=domain,DC=com")
objGroup.SetInfo

Set objGroup = GetObject("LDAP://CN=Everybody,OU=Distribution_Groups,OU=Exchange,OU=Resources,OU=Lei,OU=UK,OU=AcmeGroup,DC=domain,DC=com")
objGroup.PutEx ADS_PROPERTY_APPEND, "member", Array("CN=" & Surname & " " & FirstName & ",OU=User_Accounts, OU=" & OUNamePt2 & ",OU=UK,OU=AcmeGroup,DC=domain,DC=com")
objGroup.SetInfo

usr.put "mail", FirstName & "." & Surname & "@acmegroup.com"
usr.setinfo

'Create user's home directory
If fso.FolderExists(ProfileServer & "" & UserName) = False Then
fso.CreateFolder(ProfileServer & "" & LCase(UserName))
End If

If fso.FileExists("C:windowssystem32dssecurity.dll") = False Then
fso.CopyFile("\SERVERSHARE$PATHTOdssecurity.dll"),("C:windowssystem32")
WshShell.Run("%comspec% /c regsvr32.exe /s C:windowssystem32dssecurity.dll")
Wscript.sleep 50000
End If

ReplaceACL ProfileServer & "" & Username,"add(" & UserName & ":F)+add(domain admins:F)"

Set WshShell = Nothing
Set fso = Nothing
Set WshNetwork = Nothing
Set usr = Nothing
Set NewShare = Nothing
Set Services = Nothing
Set SecDescClass = Nothing
Set SecDesc = Nothing
Set Share = Nothing
Set InParam = Nothing
Set sec = Nothing
Set sd = Nothing
Set dacl = Nothing
Set ace = Nothing
Set oIADSUser = Nothing
Set objGroup = Nothing

MsgBox "The creation of user: " & FirstName & " " & Surname & VbCrLf &_
"has completed without error"

'Functions

'Set permissions on user's home directory
Function ReplaceACL(foldernm, permspart)
foldernm = ProfileServer & "" & Username
If fso.FolderExists(foldernm)= False Then
MsgBox "Sorry this folder is not present on the server"
Else
ChangeACLS foldernm, permspart, "REPLACE", "FOLDER"
End If
End Function

'Edit ACLS of specified folder
Function ChangeAcls(FILE,PERMS,REDIT,FFOLDER)

Const ADS_ACETYPE_ACCESS_ALLOWED = 0
Const ADS_ACETYPE_ACCESS_DENIED = 1
Const ADS_ACEFLAG_INHERIT_ACE = 2
Const ADS_ACEFLAG_SUB_NEW = 9
   
Set sec = Wscript.CreateObject("ADsSecurity")
Set sd = sec.GetSecurityDescriptor("FILE://" & FILE)
Set dacl = sd.DiscretionaryAcl

If UCase(REDIT)="REPLACE" Then
For Each existingAce In dacl
dacl.removeace existingace
Next
End If
   
'break up Perms into individual actions
cmdArray=split(perms,"+")

For x=0 to ubound(cmdarray)
tmpVar1=cmdarray(x)
If UCase(left(tmpVar1,3))="DEL" Then
ACLAction="DEL"
Else
ACLAction="ADD"
End If

tmpcmdVar=left(tmpVar1,len(tmpVar1)-1)
tmpcmdVar=right(tmpcmdVar,len(tmpcmdVar)-4)
cmdparts=split(tmpcmdVar,":")
nameVar=cmdparts(0)
rightVar=cmdparts(1)

If ACLAction="ADD" Then
If UCase(FFOLDER)="FOLDER" Then
   addace dacl, namevar, rightvar, ADS_ACETYPE_ACCESS_ALLOWED, ADS_ACEFLAG_SUB_NEW
   addace dacl, namevar, rightvar, ADS_ACETYPE_ACCESS_ALLOWED, ADS_ACEFLAG_INHERIT_ACE
Else
   addace dacl, namevar, rightvar, ADS_ACETYPE_ACCESS_ALLOWED,0
End If
End If
Next

For Each ace in dacl
If instr(ucase(ace.trustee),"NT AUTHORITY") then
   newtrustee=right(ace.trustee, len(ace.trustee)-instr(ace.trustee, ""))
   ace.trustee=newtrustee
End If
Next

sd.DiscretionaryAcl = dacl
sec.SetSecurityDescriptor sd

End Function

Function addace(dacl,trustee, maskvar, acetype, aceflags)
' add ace to the specified dacl
Const RIGHT_READ = &H80000000
Const RIGHT_EXECUTE = &H20000000
Const RIGHT_WRITE = &H40000000
Const RIGHT_DELETE = &H10000
Const RIGHT_FULL = &H10000000
Const RIGHT_CHANGE_PERMS = &H40000
Const RIGHT_TAKE_OWNERSHIP = &H80000
   
Set ace = CreateObject("AccessControlEntry")
ace.Trustee = trustee

Select Case UCase(MaskVar)
Case "F"
ace.AccessMask = RIGHT_FULL
Case "C"
ace.AccessMask = RIGHT_READ or RIGHT_WRITE or RIGHT_EXECUTE or RIGHT_DELETE
Case "R"
ace.AccessMask = RIGHT_READ or RIGHT_EXECUTE
End Select

ace.AceType = acetype
ace.AceFlags = aceflags
dacl.AddAce ace
End Function