@@ -0,0 +1,268 @@ |
| 1 | +# sultree |
| 2 | + |
| 3 | +A SELinux-aware variant of the `tree` command that filters directory trees based on SELinux security contexts. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **Full tree compatibility**: Supports all standard `tree` command options |
| 8 | +- **SELinux filtering**: Filter files and directories by SELinux security contexts using the `-S` flag |
| 9 | +- **Pattern matching**: Support for wildcard patterns in SELinux context matching |
| 10 | +- **Security focused**: Designed with security best practices and safe defaults |
| 11 | +- **Memory efficient**: Uses iterative traversal for large directory trees |
| 12 | +- **Safe error handling**: Gracefully handles permission denied and broken symlinks |
| 13 | + |
| 14 | +## Requirements |
| 15 | + |
| 16 | +- Python 3.8+ |
| 17 | +- Linux system with SELinux enabled (optional for basic tree functionality) |
| 18 | +- `getfattr` utility (part of `attr` package) |
| 19 | + |
| 20 | +## Installation |
| 21 | + |
| 22 | +### Quick Start (No Installation) |
| 23 | + |
| 24 | +You can run sultree directly from the source directory: |
| 25 | + |
| 26 | +```bash |
| 27 | +cd sultree |
| 28 | +./sultree /path/to/directory |
| 29 | +``` |
| 30 | + |
| 31 | +### Development Installation (Editable) |
| 32 | + |
| 33 | +```bash |
| 34 | +cd sultree |
| 35 | +pip install -e .[dev] |
| 36 | +``` |
| 37 | + |
| 38 | +This installs sultree in editable mode, so changes to the source code are immediately available. |
| 39 | + |
| 40 | +### Regular Installation |
| 41 | + |
| 42 | +```bash |
| 43 | +cd sultree |
| 44 | +pip install . |
| 45 | +``` |
| 46 | + |
| 47 | +## Usage |
| 48 | + |
| 49 | +### Basic Tree Functionality |
| 50 | + |
| 51 | +```bash |
| 52 | +# Basic directory tree (like standard tree) |
| 53 | +sultree /etc |
| 54 | + |
| 55 | +# Show hidden files |
| 56 | +sultree -a /home/user |
| 57 | + |
| 58 | +# Directories only, depth limited |
| 59 | +sultree -d -L 2 /usr |
| 60 | + |
| 61 | +# Show full paths |
| 62 | +sultree -f -L 1 /var/log |
| 63 | + |
| 64 | +# Follow symbolic links |
| 65 | +sultree -l /usr/local |
| 66 | +``` |
| 67 | + |
| 68 | +### SELinux Filtering |
| 69 | + |
| 70 | +```bash |
| 71 | +# Show only files with specific SELinux type |
| 72 | +sultree -S passwd_file_t /etc |
| 73 | + |
| 74 | +# Wildcard patterns in any part of context |
| 75 | +sultree -S "*admin*" /var/log |
| 76 | +sultree -S "httpd_*" /var/www |
| 77 | +sultree -S "*_exec_t" /usr/bin |
| 78 | + |
| 79 | +# Multiple SELinux patterns (OR logic) |
| 80 | +sultree -S passwd_file_t -S shadow_t /etc |
| 81 | + |
| 82 | +# Full context pattern matching |
| 83 | +sultree -S "system_u:object_r:*:s0" /etc |
| 84 | + |
| 85 | +# Combine SELinux filtering with tree options |
| 86 | +sultree -d -S httpd_exec_t -L 1 /usr/sbin |
| 87 | +sultree -a -S "*_config_t" /etc |
| 88 | +``` |
| 89 | + |
| 90 | +### Pattern Matching |
| 91 | + |
| 92 | +```bash |
| 93 | +# Include only certain file patterns |
| 94 | +sultree -P "*.conf" /etc |
| 95 | + |
| 96 | +# Exclude backup files |
| 97 | +sultree -I "*.bak" -I "*~" /home/user |
| 98 | + |
| 99 | +# Case-insensitive matching |
| 100 | +sultree --ignore-case -P "*.TXT" /tmp |
| 101 | + |
| 102 | +# Apply patterns to directories too |
| 103 | +sultree --match-dirs -P "*ssl*" /etc |
| 104 | +``` |
| 105 | + |
| 106 | +### Advanced Options |
| 107 | + |
| 108 | +```bash |
| 109 | +# Limit files per directory (performance) |
| 110 | +sultree --filelimit 100 /usr |
| 111 | + |
| 112 | +# Stay on one filesystem |
| 113 | +sultree -x / |
| 114 | + |
| 115 | +# Suppress file/directory count |
| 116 | +sultree --no-report /etc |
| 117 | +``` |
| 118 | + |
| 119 | +## SELinux Context Display |
| 120 | + |
| 121 | +When using SELinux filtering (`-S` option), sultree automatically displays the SELinux security contexts: |
| 122 | + |
| 123 | +```bash |
| 124 | +$ sultree -S passwd_file_t /etc |
| 125 | +etc |
| 126 | +group [system_u:object_r:passwd_file_t:s0] |
| 127 | +group- [system_u:object_r:passwd_file_t:s0] |
| 128 | +passwd [system_u:object_r:passwd_file_t:s0] |
| 129 | +passwd- [system_u:object_r:passwd_file_t:s0] |
| 130 | + |
| 131 | +4 files |
| 132 | +``` |
| 133 | + |
| 134 | +## Security Considerations |
| 135 | + |
| 136 | +- **Input validation**: All paths and patterns are validated to prevent injection attacks |
| 137 | +- **Safe system calls**: Uses `getfattr` directly, no shell command execution |
| 138 | +- **Path canonicalization**: Prevents directory traversal attacks |
| 139 | +- **Symlink loop detection**: Safely handles circular symlink references |
| 140 | +- **Permission handling**: Gracefully handles permission denied errors |
| 141 | +- **Memory safety**: Iterative processing prevents memory exhaustion on large trees |
| 142 | +- **Error information**: Careful not to leak sensitive information in error messages |
| 143 | + |
| 144 | +## Error Handling |
| 145 | + |
| 146 | +sultree handles various error conditions gracefully: |
| 147 | + |
| 148 | +- **Permission denied**: Warns and continues with accessible files |
| 149 | +- **Broken symlinks**: Logs and skips broken symbolic links |
| 150 | +- **SELinux unavailable**: Clear error message if SELinux filtering requested but not available |
| 151 | +- **Invalid patterns**: Validates and sanitizes all user input |
| 152 | +- **Large directories**: File limit option prevents overwhelming output |
| 153 | + |
| 154 | +## Development |
| 155 | + |
| 156 | +### Project Structure |
| 157 | + |
| 158 | +``` |
| 159 | +sultree/ |
| 160 | +├── src/sultree/ |
| 161 | +│ ├── __init__.py # Package initialization |
| 162 | +│ ├── __main__.py # CLI entry point |
| 163 | +│ ├── cli.py # Main CLI orchestration |
| 164 | +│ ├── args.py # Argument parsing |
| 165 | +│ ├── selinux.py # SELinux functionality |
| 166 | +│ ├── traversal.py # Directory traversal |
| 167 | +│ └── formatting.py # Tree output formatting |
| 168 | +├── tests/ # Test suite |
| 169 | +├── pyproject.toml # Modern Python packaging |
| 170 | +├── sultree # Standalone script |
| 171 | +└── README.md # This file |
| 172 | +``` |
| 173 | + |
| 174 | +### Running Tests |
| 175 | + |
| 176 | +```bash |
| 177 | +# Using unittest (no external dependencies) |
| 178 | +PYTHONPATH=src python3 -m unittest discover tests -v |
| 179 | + |
| 180 | +# Or with pytest (if available) |
| 181 | +pytest tests/ -v |
| 182 | +``` |
| 183 | + |
| 184 | +### Security Scanning |
| 185 | + |
| 186 | +```bash |
| 187 | +# Security linting with bandit |
| 188 | +bandit -r src/ |
| 189 | + |
| 190 | +# Dependency scanning with safety (if available) |
| 191 | +safety check |
| 192 | +``` |
| 193 | + |
| 194 | +### Code Quality |
| 195 | + |
| 196 | +```bash |
| 197 | +# Type checking with mypy |
| 198 | +mypy src/ |
| 199 | + |
| 200 | +# Code formatting with black |
| 201 | +black src/ tests/ |
| 202 | + |
| 203 | +# Linting with flake8 |
| 204 | +flake8 src/ |
| 205 | +``` |
| 206 | + |
| 207 | +## License |
| 208 | + |
| 209 | +MIT License - see pyproject.toml for details. |
| 210 | + |
| 211 | +## Contributing |
| 212 | + |
| 213 | +1. Fork the repository |
| 214 | +2. Create a feature branch |
| 215 | +3. Add tests for new functionality |
| 216 | +4. Ensure all tests pass |
| 217 | +5. Submit a pull request |
| 218 | + |
| 219 | +## Compatibility |
| 220 | + |
| 221 | +sultree aims for compatibility with the standard `tree` command while adding SELinux functionality. Most `tree` options are supported with the same behavior. |
| 222 | + |
| 223 | +### Supported tree Options |
| 224 | + |
| 225 | +- `-a, --all`: Show all files including hidden |
| 226 | +- `-d, --dirs-only`: List directories only |
| 227 | +- `-l, --follow-links`: Follow symbolic links |
| 228 | +- `-f, --full-path`: Print full path prefix |
| 229 | +- `-x, --one-file-system`: Stay on current filesystem |
| 230 | +- `-L level`: Descend only level directories deep |
| 231 | +- `-P pattern`: List only files matching pattern |
| 232 | +- `-I pattern`: Ignore files matching pattern |
| 233 | +- `--match-dirs`: Include directory names in pattern matching |
| 234 | +- `--ignore-case`: Case insensitive pattern matching |
| 235 | +- `--filelimit N`: Don't descend dirs with more than N files |
| 236 | +- `--no-report`: Turn off file/directory count |
| 237 | + |
| 238 | +### SELinux Extensions |
| 239 | + |
| 240 | +- `-S pattern, --selinux pattern`: Show only files matching SELinux pattern |
| 241 | + |
| 242 | +## Examples |
| 243 | + |
| 244 | +### System Administration |
| 245 | + |
| 246 | +```bash |
| 247 | +# Find all executable files in /usr/bin |
| 248 | +sultree -S "*_exec_t" -L 1 /usr/bin |
| 249 | + |
| 250 | +# Audit configuration files with specific contexts |
| 251 | +sultree -S "*_config_t" /etc |
| 252 | + |
| 253 | +# Check for files with admin contexts |
| 254 | +sultree -S "*admin*" /var/log |
| 255 | +``` |
| 256 | + |
| 257 | +### Security Analysis |
| 258 | + |
| 259 | +```bash |
| 260 | +# Find files with user contexts in system directories |
| 261 | +sultree -S "user_*" /etc /var |
| 262 | + |
| 263 | +# Look for temporary file contexts |
| 264 | +sultree -S "*tmp*" /var /tmp |
| 265 | + |
| 266 | +# Audit files accessible to specific domains |
| 267 | +sultree -S "httpd_*" /var/www /etc/httpd |
| 268 | +``` |