Project 1 audit user

This commit is contained in:
2026-01-26 08:39:36 +01:00
parent 3e23329086
commit 20dbbbdd77
5 changed files with 104 additions and 2 deletions

View File

@@ -1,9 +1,7 @@
if the number of user dobules if the number of user dobules
the time of execution will be multiply by 2 the time of execution will be multiply by 2
$$ $$
T(n, m) = n \times m T(n, m) = n \times m
T(2n, m) = 2 \times n \times m T(2n, m) = 2 \times n \times m

View File

@@ -0,0 +1,28 @@
# PowerShell User Audit Script
# Get all local usernames as strings
#! Get-LocalUser returns objects, not just strings.
#! -split $users is incorrect for objects.
$users = Get-LocalUser | Select-Object -ExpandProperty Name
foreach ($username in $users) {
$badChar = $false
# Check each character
foreach ($char in $username.ToCharArray()) {
if ($char -notmatch "[A-Za-z]") {
$badChar = $true
break
}
}
# Check first character
$firstChar = $username[0]
$validFirstLetter = $firstChar -match "[A-Za-z]"
# Flag invalid usernames
# * Note: -ge 20 includes length 20. If you want max length 20 valid, use -gt 20.
if ($badChar -or $username.Length -gt 20 -or -not $validFirstLetter) {
Write-Host "The username '$username' is invalid"
}
}

View File

@@ -0,0 +1,26 @@
#!/bin/bash
# userAudit: print invalid usernames
#! Safe iteration over /etc/passwd using while read -r
awk -F ":" '{print $1}' /etc/passwd | while read -r username; do
firstChar="${username:0:1}"
badChar=false
# Check if username contains only letters
for ((i=0; i<${#username}; i++)); do
char="${username:i:1}"
if ! [[ "$char" =~ [A-Za-z] ]]; then
badChar=true
break
fi
done
# Flag invalid usernames
#! Corrected logic to properly flag invalid usernames:
#! check if the firstchar is NOT a letter
if (( ${#username} > 20 )) || ! [[ "$firstChar" =~ [A-Za-z] ]] || $badChar ; then
#! Quotes around $username in echo for robustness.
echo "$username"
fi
done

View File

@@ -0,0 +1,21 @@
$users = Get-LocalUser
foreach($username in -split $users){
$badChar = $false
foreach ($char in $username.ToCharArray()) {
if($char -notmatch "[A-Za-z]"){
$badChar = $true
break
}
}
$fistChar = $username[0]
$validFirstLetter = $fistChar -match "[A-Za-z]"
if ($badChar -or $username.Length -ge 20 -or -not $validFirstLetter ){
Write-Host "the userName " + $username + " is invalid"
}
}

View File

@@ -0,0 +1,29 @@
#!/bin/bash
# userAudit
#Read the passwd file for all the username
file=$(awk -F ":" '{print $1}' /etc/passwd)
for username in $file; do
firstChar="${username:0:1}"
badChar=false
#check if the username contain only letters
for ((i=0; i<${#username}; i++)); do
char="${username:i:1}"
if ! [[ "$char" =~ [A-Za-z] ]]; then
badChar=true
break
fi
done
#if the username is not valid
# longer than 20 characters, start with not a letter and contain other char than letter
# print it
if ((${#username} >= 20)) || ! [[ "$firstChar" =~ [A-Za-z] ]] || $badChar ; then
echo $username
fi
done