-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathfix_permissions.ps1
More file actions
152 lines (136 loc) · 5.33 KB
/
fix_permissions.ps1
File metadata and controls
152 lines (136 loc) · 5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# PowerShell script to fix permissions for Ragnar repository
# This script sets appropriate permissions for the Ragnar project files and directories
Write-Host "Fixing permissions for Ragnar repository..." -ForegroundColor Green
# Get the current directory (should be Ragnar repo root)
$repoPath = Get-Location
# Directories that need write permissions
$writableDirs = @(
"data",
"data\networks",
"data\logs",
"data\input",
"data\intelligence",
"data\network_data",
"data\threat_intelligence",
"config",
"resources\comments",
"var\log",
"web"
)
# Files that need write permissions
$writableFiles = @(
"data\networks\.last_ssid",
"config\shared_config.json",
"config\actions.json"
)
# Set permissions for directories
foreach ($dir in $writableDirs) {
$fullPath = Join-Path $repoPath $dir
if (Test-Path $fullPath) {
Write-Host "Setting permissions for directory: $dir" -ForegroundColor Yellow
try {
# Remove read-only attribute recursively
Get-ChildItem -Path $fullPath -Recurse -Force | ForEach-Object {
if ($_.Attributes -band [System.IO.FileAttributes]::ReadOnly) {
$_.Attributes = $_.Attributes -band (-bnot [System.IO.FileAttributes]::ReadOnly)
}
}
# Set full control for current user
$acl = Get-Acl $fullPath
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule(
[System.Security.Principal.WindowsIdentity]::GetCurrent().Name,
"FullControl",
"ContainerInherit,ObjectInherit",
"None",
"Allow"
)
$acl.SetAccessRule($accessRule)
Set-Acl -Path $fullPath -AclObject $acl
Write-Host "✅ Fixed permissions for $dir" -ForegroundColor Green
}
catch {
Write-Host "❌ Failed to set permissions for $dir`: $($_.Exception.Message)" -ForegroundColor Red
}
} else {
Write-Host "Creating directory: $dir" -ForegroundColor Cyan
try {
New-Item -Path $fullPath -ItemType Directory -Force | Out-Null
Write-Host "✅ Created directory $dir" -ForegroundColor Green
}
catch {
Write-Host "❌ Failed to create directory $dir`: $($_.Exception.Message)" -ForegroundColor Red
}
}
}
# Set permissions for specific files
foreach ($file in $writableFiles) {
$fullPath = Join-Path $repoPath $file
$parentDir = Split-Path $fullPath -Parent
# Create parent directory if it doesn't exist
if (-not (Test-Path $parentDir)) {
Write-Host "Creating parent directory for: $file" -ForegroundColor Cyan
New-Item -Path $parentDir -ItemType Directory -Force | Out-Null
}
# Create file if it doesn't exist
if (-not (Test-Path $fullPath)) {
Write-Host "Creating file: $file" -ForegroundColor Cyan
try {
New-Item -Path $fullPath -ItemType File -Force | Out-Null
}
catch {
Write-Host "❌ Failed to create file $file`: $($_.Exception.Message)" -ForegroundColor Red
continue
}
}
Write-Host "Setting permissions for file: $file" -ForegroundColor Yellow
try {
# Remove read-only attribute
$fileItem = Get-Item $fullPath -Force
if ($fileItem.Attributes -band [System.IO.FileAttributes]::ReadOnly) {
$fileItem.Attributes = $fileItem.Attributes -band (-bnot [System.IO.FileAttributes]::ReadOnly)
}
# Set full control for current user
$acl = Get-Acl $fullPath
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule(
[System.Security.Principal.WindowsIdentity]::GetCurrent().Name,
"FullControl",
"Allow"
)
$acl.SetAccessRule($accessRule)
Set-Acl -Path $fullPath -AclObject $acl
Write-Host "✅ Fixed permissions for $file" -ForegroundColor Green
}
catch {
Write-Host "❌ Failed to set permissions for $file`: $($_.Exception.Message)" -ForegroundColor Red
}
}
# Fix .gitignore to ensure data files are properly handled
$gitignorePath = Join-Path $repoPath ".gitignore"
if (Test-Path $gitignorePath) {
Write-Host "Checking .gitignore patterns..." -ForegroundColor Yellow
$gitignoreContent = Get-Content $gitignorePath -Raw
$requiredPatterns = @(
"data/networks/.last_ssid",
"data/logs/*.log",
"data/networks/*/livestatus.csv",
"data/networks/*/netkb.csv",
"*.pyc",
"__pycache__/",
".env"
)
$modified = $false
foreach ($pattern in $requiredPatterns) {
if ($gitignoreContent -notlike "*$pattern*") {
Write-Host "Adding pattern to .gitignore: $pattern" -ForegroundColor Cyan
Add-Content -Path $gitignorePath -Value $pattern
$modified = $true
}
}
if ($modified) {
Write-Host "✅ Updated .gitignore with required patterns" -ForegroundColor Green
} else {
Write-Host "✅ .gitignore already contains required patterns" -ForegroundColor Green
}
}
Write-Host "`n🎉 Permission fix completed!" -ForegroundColor Green
Write-Host "You can now sync these changes to your Raspberry Pi." -ForegroundColor Cyan