-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
1083 lines (927 loc) · 34.2 KB
/
Copy pathconfig.go
File metadata and controls
1083 lines (927 loc) · 34.2 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"bufio"
"crypto/rand"
"encoding/base64"
"encoding/json"
"fmt"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"sync"
"time"
"os/exec"
"golang.org/x/crypto/bcrypt"
)
// SSHServerConfig represents the actual SSH daemon configuration
// This is read from and written to the actual sshd_config file
type SSHServerConfig struct {
Port int `json:"port"`
PermitRootLogin string `json:"permit_root_login"` // yes, no, prohibit-password
PasswordAuthentication bool `json:"password_authentication"`
PubkeyAuthentication bool `json:"pubkey_authentication"`
AllowUsers string `json:"allow_users"` // Space-separated list
DenyUsers string `json:"deny_users"` // Space-separated list
ClientAliveInterval int `json:"client_alive_interval"` // Seconds
ClientAliveCountMax int `json:"client_alive_count_max"`
MaxAuthTries int `json:"max_auth_tries"`
MaxSessions int `json:"max_sessions"`
MaxStartups string `json:"max_startups"` // e.g., "10:30:60"
LoginGraceTime int `json:"login_grace_time"` // Seconds
Banner string `json:"banner"` // Path to banner file
PrintMotd bool `json:"print_motd"`
PrintLastLog bool `json:"print_last_log"`
TCPKeepAlive bool `json:"tcp_keep_alive"`
X11Forwarding bool `json:"x11_forwarding"`
AllowTcpForwarding string `json:"allow_tcp_forwarding"` // yes, no, local, remote
GatewayPorts string `json:"gateway_ports"` // yes, no, clientspecified
PermitTunnel string `json:"permit_tunnel"` // yes, no, point-to-point, ethernet
// Advanced options
Protocol string `json:"protocol"` // 2 (SSH2 only recommended)
LogLevel string `json:"log_level"` // QUIET, FATAL, ERROR, INFO, VERBOSE, DEBUG
SyslogFacility string `json:"syslog_facility"` // AUTH, AUTHPRIV, DAEMON, etc.
StrictModes bool `json:"strict_modes"`
IgnoreRhosts bool `json:"ignore_rhosts"`
HostbasedAuthentication bool `json:"hostbased_authentication"`
PermitEmptyPasswords bool `json:"permit_empty_passwords"`
ChallengeResponseAuthentication bool `json:"challenge_response_authentication"`
KerberosAuthentication bool `json:"kerberos_authentication"`
GSSAPIAuthentication bool `json:"gssapi_authentication"`
UsePAM bool `json:"use_pam"`
// File paths - these are constant system paths
ConfigPath string `json:"config_path"`
AuthorizedKeysFile string `json:"authorized_keys_file"`
HostKeyPath string `json:"host_key_path"`
}
// SSHServerDiagnostics represents potential issues with SSH server installation
type SSHServerDiagnostics struct {
ConfigFileExists bool `json:"config_file_exists"`
ConfigFilePath string `json:"config_file_path"`
SSHDInstalled bool `json:"sshd_installed"`
SSHServiceName string `json:"ssh_service_name"` // "ssh", "sshd", or ""
MissingPackages []string `json:"missing_packages"`
Suggestions []string `json:"suggestions"`
PermissionIssues bool `json:"permission_issues"`
SELinuxIssues bool `json:"selinux_issues"`
}
type SSHKeyPair struct {
Name string `json:"name"`
Type string `json:"type"` // rsa, ed25519, ecdsa
Bits int `json:"bits"` // Key size for RSA/ECDSA
Comment string `json:"comment"`
PublicKey string `json:"public_key"` // Content of .pub file
PrivateKeyPath string `json:"private_key_path"` // Path to private key file
CreatedAt string `json:"created_at"`
LastUsed string `json:"last_used"`
Notes string `json:"notes"`
}
type WebConfig struct {
Port int `json:"port"`
Password string `json:"password"`
SessionTimeout int `json:"session_timeout"` // Minutes
DefaultPasswordChanged bool `json:"default_password_changed"` // Track if admin/admin was changed
SecurityAcknowledged bool `json:"security_acknowledged"` // Track if user acknowledged security warnings
}
type AppConfig struct {
KeyPairs map[string]SSHKeyPair `json:"key_pairs"`
Web WebConfig `json:"web"`
// SSH server config file path (for reading actual config)
SSHConfigPath string `json:"ssh_config_path"`
// Server config - populated dynamically from actual SSH config file
// This is not stored in JSON but loaded from the actual sshd_config
Server SSHServerConfig `json:"-"`
}
// Session represents an active user session
type Session struct {
ID string `json:"id"`
UserID string `json:"user_id"`
CreatedAt time.Time `json:"created_at"`
ExpiresAt time.Time `json:"expires_at"`
}
// SessionManager handles user sessions
type SessionManager struct {
sessions map[string]*Session
mutex sync.RWMutex
}
// Simple rate limiting for login attempts
type LoginAttempt struct {
FailedAttempts int
LastAttempt time.Time
BlockedUntil time.Time
}
type RateLimiter struct {
attempts map[string]*LoginAttempt
mutex sync.RWMutex
}
// Global instances
var sessionManager = &SessionManager{
sessions: make(map[string]*Session),
}
var rateLimiter = &RateLimiter{
attempts: make(map[string]*LoginAttempt),
}
const defaultConfigPath = "ssh-pilot.json"
func loadConfig() (*AppConfig, error) {
config := &AppConfig{
KeyPairs: make(map[string]SSHKeyPair),
SSHConfigPath: "/etc/ssh/sshd_config", // Default SSH config path
Web: WebConfig{
Port: 8081,
Password: "admin",
SessionTimeout: 60,
},
}
if _, err := os.Stat(defaultConfigPath); os.IsNotExist(err) {
// Hash the default password before returning
if hashedPassword, err := HashPassword(config.Web.Password); err == nil {
config.Web.Password = hashedPassword
}
return config, nil
}
data, err := os.ReadFile(defaultConfigPath)
if err != nil {
return nil, fmt.Errorf("failed to read config file: %v", err)
}
if err := json.Unmarshal(data, config); err != nil {
return nil, fmt.Errorf("failed to parse config file: %v", err)
}
// Migrate old configs and set defaults
if config.Web.Port == 0 {
config.Web.Port = 8081
}
if config.Web.SessionTimeout == 0 {
config.Web.SessionTimeout = 60
}
// SECURITY: Migrate plaintext passwords to hashed passwords
if !strings.HasPrefix(config.Web.Password, "$2a$") && !strings.HasPrefix(config.Web.Password, "$2b$") {
if hashedPassword, err := HashPassword(config.Web.Password); err == nil {
config.Web.Password = hashedPassword
if saveErr := config.save(); saveErr != nil {
fmt.Printf("Warning: Failed to save hashed password to config: %v\n", saveErr)
}
} else {
fmt.Printf("Warning: Failed to hash password: %v\n", err)
}
}
// Load SSH server configuration from actual file
config.refreshServerConfig()
return config, nil
}
func (c *AppConfig) save() error {
data, err := json.MarshalIndent(c, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal config: %v", err)
}
if err := os.WriteFile(defaultConfigPath, data, 0600); err != nil {
return fmt.Errorf("failed to write config file: %v", err)
}
return nil
}
func ensureConfigDir() error {
dir := filepath.Dir(defaultConfigPath)
return os.MkdirAll(dir, 0755)
}
// Rate limiter methods
func (rl *RateLimiter) IsBlocked(ip string) bool {
rl.mutex.RLock()
defer rl.mutex.RUnlock()
attempt, exists := rl.attempts[ip]
if !exists {
return false
}
return time.Now().Before(attempt.BlockedUntil)
}
func (rl *RateLimiter) RecordFailedAttempt(ip string) {
rl.mutex.Lock()
defer rl.mutex.Unlock()
attempt, exists := rl.attempts[ip]
if !exists {
attempt = &LoginAttempt{}
rl.attempts[ip] = attempt
}
attempt.FailedAttempts++
attempt.LastAttempt = time.Now()
// Block for 15 minutes after 5 failed attempts
if attempt.FailedAttempts >= 5 {
attempt.BlockedUntil = time.Now().Add(15 * time.Minute)
}
}
func (rl *RateLimiter) RecordSuccessfulLogin(ip string) {
rl.mutex.Lock()
defer rl.mutex.Unlock()
delete(rl.attempts, ip)
}
func (rl *RateLimiter) CleanupOldAttempts() {
rl.mutex.Lock()
defer rl.mutex.Unlock()
cutoff := time.Now().Add(-1 * time.Hour)
for ip, attempt := range rl.attempts {
if attempt.LastAttempt.Before(cutoff) && time.Now().After(attempt.BlockedUntil) {
delete(rl.attempts, ip)
}
}
}
// Session management functions
func generateSessionID() (string, error) {
bytes := make([]byte, 32)
if _, err := rand.Read(bytes); err != nil {
return "", err
}
return base64.URLEncoding.EncodeToString(bytes), nil
}
func (sm *SessionManager) CreateSession(userID string, timeoutMinutes int) (*Session, error) {
sm.mutex.Lock()
defer sm.mutex.Unlock()
sessionID, err := generateSessionID()
if err != nil {
return nil, err
}
now := time.Now()
session := &Session{
ID: sessionID,
UserID: userID,
CreatedAt: now,
ExpiresAt: now.Add(time.Duration(timeoutMinutes) * time.Minute),
}
sm.sessions[sessionID] = session
return session, nil
}
func (sm *SessionManager) GetSession(sessionID string) (*Session, bool) {
sm.mutex.RLock()
defer sm.mutex.RUnlock()
session, exists := sm.sessions[sessionID]
if !exists || time.Now().After(session.ExpiresAt) {
if exists {
delete(sm.sessions, sessionID)
}
return nil, false
}
return session, true
}
func (sm *SessionManager) DeleteSession(sessionID string) {
sm.mutex.Lock()
defer sm.mutex.Unlock()
delete(sm.sessions, sessionID)
}
func (sm *SessionManager) CleanupExpiredSessions() {
sm.mutex.Lock()
defer sm.mutex.Unlock()
now := time.Now()
for id, session := range sm.sessions {
if now.After(session.ExpiresAt) {
delete(sm.sessions, id)
}
}
}
// Password utilities
func ValidatePassword(provided, stored string) bool {
if !strings.HasPrefix(stored, "$2a$") && !strings.HasPrefix(stored, "$2b$") {
return provided == stored
}
err := bcrypt.CompareHashAndPassword([]byte(stored), []byte(provided))
return err == nil
}
func HashPassword(password string) (string, error) {
bytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
return string(bytes), err
}
// SSH Key management
func (c *AppConfig) addKeyPair(name string) error {
if _, exists := c.KeyPairs[name]; exists {
return fmt.Errorf("key pair %s already exists", name)
}
keyPair := SSHKeyPair{
Name: name,
Type: "ed25519",
Bits: 0, // ed25519 doesn't use bits
Comment: "",
PublicKey: "",
PrivateKeyPath: "",
CreatedAt: time.Now().Format(time.RFC3339),
LastUsed: "",
Notes: "",
}
c.KeyPairs[name] = keyPair
return nil
}
func (c *AppConfig) removeKeyPair(name string) error {
if _, exists := c.KeyPairs[name]; !exists {
return fmt.Errorf("key pair %s does not exist", name)
}
delete(c.KeyPairs, name)
return nil
}
// SSH Configuration file parsing functions
// parseBoolValue parses SSH config boolean values (yes/no, true/false, on/off)
func parseBoolValue(value string) bool {
value = strings.ToLower(strings.TrimSpace(value))
return value == "yes" || value == "true" || value == "on"
}
// parseIntValue safely parses integer values with fallback
func parseIntValue(value string, defaultVal int) int {
if value == "" {
return defaultVal
}
if parsed, err := strconv.Atoi(strings.TrimSpace(value)); err == nil {
return parsed
}
return defaultVal
}
// ReadSSHServerConfig reads and parses the actual sshd_config file
func (c *AppConfig) ReadSSHServerConfig() (*SSHServerConfig, error) {
configPath := c.SSHConfigPath
// Check if config file exists
if _, err := os.Stat(configPath); os.IsNotExist(err) {
return nil, fmt.Errorf("SSH config file does not exist: %s", configPath)
}
file, err := os.Open(configPath)
if err != nil {
return nil, fmt.Errorf("failed to open SSH config file: %v", err)
}
defer file.Close()
// Default configuration values
config := &SSHServerConfig{
Port: 22,
PermitRootLogin: "prohibit-password",
PasswordAuthentication: true,
PubkeyAuthentication: true,
AllowUsers: "",
DenyUsers: "",
ClientAliveInterval: 0,
ClientAliveCountMax: 3,
MaxAuthTries: 6,
MaxSessions: 10,
MaxStartups: "10:30:60",
LoginGraceTime: 120,
Banner: "",
PrintMotd: true,
PrintLastLog: true,
TCPKeepAlive: true,
X11Forwarding: false,
AllowTcpForwarding: "yes",
GatewayPorts: "no",
PermitTunnel: "no",
Protocol: "2",
LogLevel: "INFO",
SyslogFacility: "AUTH",
StrictModes: true,
IgnoreRhosts: true,
HostbasedAuthentication: false,
PermitEmptyPasswords: false,
ChallengeResponseAuthentication: false,
KerberosAuthentication: false,
GSSAPIAuthentication: false,
UsePAM: true,
ConfigPath: configPath,
AuthorizedKeysFile: ".ssh/authorized_keys",
HostKeyPath: "/etc/ssh/ssh_host_ed25519_key",
}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
// Skip comments and empty lines
if line == "" || strings.HasPrefix(line, "#") {
continue
}
// Split key and value
fields := strings.Fields(line)
if len(fields) < 2 {
continue
}
key := strings.ToLower(fields[0])
value := strings.Join(fields[1:], " ")
// Parse configuration values
switch key {
case "port":
config.Port = parseIntValue(value, 22)
case "permitrootlogin":
config.PermitRootLogin = strings.ToLower(value)
case "passwordauthentication":
config.PasswordAuthentication = parseBoolValue(value)
case "pubkeyauthentication":
config.PubkeyAuthentication = parseBoolValue(value)
case "allowusers":
config.AllowUsers = value
case "denyusers":
config.DenyUsers = value
case "clientaliveinterval":
config.ClientAliveInterval = parseIntValue(value, 0)
case "clientalivecountmax":
config.ClientAliveCountMax = parseIntValue(value, 3)
case "maxauthtries":
config.MaxAuthTries = parseIntValue(value, 6)
case "maxsessions":
config.MaxSessions = parseIntValue(value, 10)
case "maxstartups":
config.MaxStartups = value
case "logingracetime":
config.LoginGraceTime = parseIntValue(value, 120)
case "banner":
config.Banner = value
case "printmotd":
config.PrintMotd = parseBoolValue(value)
case "printlastlog":
config.PrintLastLog = parseBoolValue(value)
case "tcpkeepalive":
config.TCPKeepAlive = parseBoolValue(value)
case "x11forwarding":
config.X11Forwarding = parseBoolValue(value)
case "allowtcpforwarding":
config.AllowTcpForwarding = strings.ToLower(value)
case "gatewayports":
config.GatewayPorts = strings.ToLower(value)
case "permittunnel":
config.PermitTunnel = strings.ToLower(value)
case "protocol":
config.Protocol = value
case "loglevel":
config.LogLevel = strings.ToUpper(value)
case "syslogfacility":
config.SyslogFacility = strings.ToUpper(value)
case "strictmodes":
config.StrictModes = parseBoolValue(value)
case "ignorerhosts":
config.IgnoreRhosts = parseBoolValue(value)
case "hostbasedauthentication":
config.HostbasedAuthentication = parseBoolValue(value)
case "permitemptypasswords":
config.PermitEmptyPasswords = parseBoolValue(value)
case "challengeresponseauthentication":
config.ChallengeResponseAuthentication = parseBoolValue(value)
case "kerberosauthentication":
config.KerberosAuthentication = parseBoolValue(value)
case "gssapiauthentication":
config.GSSAPIAuthentication = parseBoolValue(value)
case "usepam":
config.UsePAM = parseBoolValue(value)
case "authorizedkeysfile":
config.AuthorizedKeysFile = value
case "hostkey":
config.HostKeyPath = value
}
}
if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("error reading SSH config file: %v", err)
}
return config, nil
}
// WriteSSHServerConfig writes the SSH server configuration to the actual sshd_config file
func (c *AppConfig) WriteSSHServerConfig(config *SSHServerConfig) error {
configPath := c.SSHConfigPath
// Check if we can write to the config file
if _, err := os.Stat(configPath); os.IsNotExist(err) {
return fmt.Errorf("SSH config file does not exist: %s", configPath)
}
// Create backup
backupPath := configPath + ".backup." + time.Now().Format("20060102_150405")
if err := copyFile(configPath, backupPath); err != nil {
return fmt.Errorf("failed to create backup: %v", err)
}
// Generate new configuration content
var content strings.Builder
content.WriteString("# SSH Daemon Configuration\n")
content.WriteString("# Generated by SSH Manager on " + time.Now().Format(time.RFC3339) + "\n")
content.WriteString("# Backup saved as: " + backupPath + "\n\n")
// Write configuration values
content.WriteString(fmt.Sprintf("Port %d\n", config.Port))
content.WriteString(fmt.Sprintf("Protocol %s\n", config.Protocol))
content.WriteString(fmt.Sprintf("LogLevel %s\n", config.LogLevel))
content.WriteString(fmt.Sprintf("SyslogFacility %s\n", config.SyslogFacility))
// Host keys
content.WriteString(fmt.Sprintf("HostKey %s\n", config.HostKeyPath))
// Authentication
content.WriteString(fmt.Sprintf("PermitRootLogin %s\n", config.PermitRootLogin))
content.WriteString(fmt.Sprintf("PasswordAuthentication %s\n", boolToYesNo(config.PasswordAuthentication)))
content.WriteString(fmt.Sprintf("PubkeyAuthentication %s\n", boolToYesNo(config.PubkeyAuthentication)))
if config.AllowUsers != "" {
content.WriteString(fmt.Sprintf("AllowUsers %s\n", config.AllowUsers))
}
if config.DenyUsers != "" {
content.WriteString(fmt.Sprintf("DenyUsers %s\n", config.DenyUsers))
}
// Connection settings
content.WriteString(fmt.Sprintf("MaxAuthTries %d\n", config.MaxAuthTries))
content.WriteString(fmt.Sprintf("MaxSessions %d\n", config.MaxSessions))
content.WriteString(fmt.Sprintf("MaxStartups %s\n", config.MaxStartups))
content.WriteString(fmt.Sprintf("LoginGraceTime %d\n", config.LoginGraceTime))
if config.ClientAliveInterval > 0 {
content.WriteString(fmt.Sprintf("ClientAliveInterval %d\n", config.ClientAliveInterval))
content.WriteString(fmt.Sprintf("ClientAliveCountMax %d\n", config.ClientAliveCountMax))
}
// Security settings
content.WriteString(fmt.Sprintf("StrictModes %s\n", boolToYesNo(config.StrictModes)))
content.WriteString(fmt.Sprintf("IgnoreRhosts %s\n", boolToYesNo(config.IgnoreRhosts)))
content.WriteString(fmt.Sprintf("HostbasedAuthentication %s\n", boolToYesNo(config.HostbasedAuthentication)))
content.WriteString(fmt.Sprintf("PermitEmptyPasswords %s\n", boolToYesNo(config.PermitEmptyPasswords)))
content.WriteString(fmt.Sprintf("ChallengeResponseAuthentication %s\n", boolToYesNo(config.ChallengeResponseAuthentication)))
// Authentication methods
content.WriteString(fmt.Sprintf("KerberosAuthentication %s\n", boolToYesNo(config.KerberosAuthentication)))
content.WriteString(fmt.Sprintf("GSSAPIAuthentication %s\n", boolToYesNo(config.GSSAPIAuthentication)))
content.WriteString(fmt.Sprintf("UsePAM %s\n", boolToYesNo(config.UsePAM)))
// Display settings
content.WriteString(fmt.Sprintf("PrintMotd %s\n", boolToYesNo(config.PrintMotd)))
content.WriteString(fmt.Sprintf("PrintLastLog %s\n", boolToYesNo(config.PrintLastLog)))
if config.Banner != "" {
content.WriteString(fmt.Sprintf("Banner %s\n", config.Banner))
}
// Network settings
content.WriteString(fmt.Sprintf("TCPKeepAlive %s\n", boolToYesNo(config.TCPKeepAlive)))
// Forwarding settings
content.WriteString(fmt.Sprintf("X11Forwarding %s\n", boolToYesNo(config.X11Forwarding)))
content.WriteString(fmt.Sprintf("AllowTcpForwarding %s\n", config.AllowTcpForwarding))
content.WriteString(fmt.Sprintf("GatewayPorts %s\n", config.GatewayPorts))
content.WriteString(fmt.Sprintf("PermitTunnel %s\n", config.PermitTunnel))
// Other settings
content.WriteString(fmt.Sprintf("AuthorizedKeysFile %s\n", config.AuthorizedKeysFile))
// Write to file
if err := os.WriteFile(configPath, []byte(content.String()), 0644); err != nil {
return fmt.Errorf("failed to write SSH config file: %v", err)
}
return nil
}
// DiagnoseSSHInstallation analyzes potential SSH installation issues
func (c *AppConfig) DiagnoseSSHInstallation() *SSHServerDiagnostics {
diag := &SSHServerDiagnostics{
ConfigFilePath: c.SSHConfigPath,
MissingPackages: []string{},
Suggestions: []string{},
PermissionIssues: false,
SELinuxIssues: false,
}
// Check if config file exists
if _, err := os.Stat(c.SSHConfigPath); os.IsNotExist(err) {
diag.ConfigFileExists = false
diag.Suggestions = append(diag.Suggestions, "SSH server configuration file is missing")
} else {
diag.ConfigFileExists = true
}
// Check if sshd binary is installed
if _, err := exec.LookPath("sshd"); err != nil {
diag.SSHDInstalled = false
diag.MissingPackages = append(diag.MissingPackages, "openssh-server")
diag.Suggestions = append(diag.Suggestions, "Install SSH server package")
} else {
diag.SSHDInstalled = true
}
// Check which service name works
if diag.SSHDInstalled {
if exec.Command("systemctl", "status", "ssh").Run() == nil {
diag.SSHServiceName = "ssh"
} else if exec.Command("systemctl", "status", "sshd").Run() == nil {
diag.SSHServiceName = "sshd"
}
}
// Provide specific suggestions based on findings
if !diag.ConfigFileExists && !diag.SSHDInstalled {
diag.Suggestions = append(diag.Suggestions, "SSH server is not installed. Install it with:")
// Detect OS and provide appropriate install command
if _, err := os.Stat("/etc/debian_version"); err == nil {
diag.Suggestions = append(diag.Suggestions, " sudo apt update && sudo apt install openssh-server")
} else if _, err := os.Stat("/etc/redhat-release"); err == nil {
diag.Suggestions = append(diag.Suggestions, " sudo yum install openssh-server # (CentOS/RHEL 7)")
diag.Suggestions = append(diag.Suggestions, " sudo dnf install openssh-server # (CentOS/RHEL 8+, Fedora)")
} else if _, err := os.Stat("/etc/arch-release"); err == nil {
diag.Suggestions = append(diag.Suggestions, " sudo pacman -S openssh")
} else {
diag.Suggestions = append(diag.Suggestions, " Use your distribution's package manager to install openssh-server")
}
diag.Suggestions = append(diag.Suggestions, "After installation, enable and start the service:")
diag.Suggestions = append(diag.Suggestions, " sudo systemctl enable ssh")
diag.Suggestions = append(diag.Suggestions, " sudo systemctl start ssh")
} else if !diag.ConfigFileExists && diag.SSHDInstalled {
diag.Suggestions = append(diag.Suggestions, "SSH server is installed but config file is missing.")
diag.Suggestions = append(diag.Suggestions, "Try reinstalling the openssh-server package:")
if _, err := os.Stat("/etc/debian_version"); err == nil {
diag.Suggestions = append(diag.Suggestions, " sudo apt reinstall openssh-server")
} else {
diag.Suggestions = append(diag.Suggestions, " Reinstall openssh-server using your package manager")
}
} else if diag.ConfigFileExists && !diag.SSHDInstalled {
diag.Suggestions = append(diag.Suggestions, "Config file exists but SSH daemon is not installed.")
diag.Suggestions = append(diag.Suggestions, "This is unusual - you may have a partial installation.")
}
// Check for permission issues
if diag.ConfigFileExists {
if info, err := os.Stat(c.SSHConfigPath); err == nil {
// Check if we can read the file
if _, err := os.Open(c.SSHConfigPath); err != nil {
diag.PermissionIssues = true
diag.Suggestions = append(diag.Suggestions, "Permission denied reading SSH config. You may need sudo privileges.")
}
// Warn about unusual permissions
if info.Mode().Perm() != 0644 && info.Mode().Perm() != 0600 {
diag.Suggestions = append(diag.Suggestions, fmt.Sprintf("Unusual file permissions on %s: %o", c.SSHConfigPath, info.Mode().Perm()))
}
}
}
// Check for SELinux issues (basic check)
if _, err := os.Stat("/sys/fs/selinux"); err == nil {
// SELinux is present, add a note
diag.Suggestions = append(diag.Suggestions, "SELinux is active. If you encounter issues, check SELinux policies for SSH.")
}
return diag
}
// SSH Configuration Backup Management
// BackupInfo represents information about a backup file
type BackupInfo struct {
Path string `json:"path"`
Filename string `json:"filename"`
CreatedAt time.Time `json:"created_at"`
Size int64 `json:"size"`
Description string `json:"description"`
IsAutomatic bool `json:"is_automatic"`
ConfigHash string `json:"config_hash,omitempty"`
}
// BackupManager manages SSH configuration backups
type BackupManager struct {
ConfigPath string
BackupDir string
MaxBackups int
RetentionDays int
}
// NewBackupManager creates a new backup manager
func (c *AppConfig) NewBackupManager() *BackupManager {
backupDir := filepath.Dir(c.SSHConfigPath)
return &BackupManager{
ConfigPath: c.SSHConfigPath,
BackupDir: backupDir,
MaxBackups: 10, // Keep last 10 backups
RetentionDays: 30, // Keep backups for 30 days
}
}
// CreateBackup creates a backup of the SSH configuration file
func (bm *BackupManager) CreateBackup(description string, isAutomatic bool) (*BackupInfo, error) {
// Check if source file exists
if _, err := os.Stat(bm.ConfigPath); os.IsNotExist(err) {
return nil, fmt.Errorf("SSH config file does not exist: %s", bm.ConfigPath)
}
// Get file info
fileInfo, err := os.Stat(bm.ConfigPath)
if err != nil {
return nil, fmt.Errorf("failed to get file info: %v", err)
}
// Generate backup filename
timestamp := time.Now().Format("20060102_150405")
var filename string
if isAutomatic {
filename = fmt.Sprintf("sshd_config.backup.%s", timestamp)
} else {
// For manual backups, include description in filename
cleanDesc := strings.ReplaceAll(strings.ToLower(description), " ", "_")
cleanDesc = strings.ReplaceAll(cleanDesc, "/", "_")
if len(cleanDesc) > 20 {
cleanDesc = cleanDesc[:20]
}
if cleanDesc != "" {
filename = fmt.Sprintf("sshd_config.backup.%s.%s", timestamp, cleanDesc)
} else {
filename = fmt.Sprintf("sshd_config.backup.%s.manual", timestamp)
}
}
backupPath := filepath.Join(bm.BackupDir, filename)
// Create backup
if err := copyFile(bm.ConfigPath, backupPath); err != nil {
return nil, fmt.Errorf("failed to create backup: %v", err)
}
// Calculate config hash for integrity checking
configContent, err := os.ReadFile(bm.ConfigPath)
if err != nil {
return nil, fmt.Errorf("failed to read config for hash: %v", err)
}
hash := fmt.Sprintf("%x", configContent[:min(len(configContent), 32)]) // Simple hash
backup := &BackupInfo{
Path: backupPath,
Filename: filename,
CreatedAt: time.Now(),
Size: fileInfo.Size(),
Description: description,
IsAutomatic: isAutomatic,
ConfigHash: hash,
}
return backup, nil
}
// ListBackups returns a list of all available backups
func (bm *BackupManager) ListBackups() ([]BackupInfo, error) {
var backups []BackupInfo
// Read directory
files, err := os.ReadDir(bm.BackupDir)
if err != nil {
return nil, fmt.Errorf("failed to read backup directory: %v", err)
}
// Find backup files
for _, file := range files {
if file.IsDir() {
continue
}
filename := file.Name()
if !strings.HasPrefix(filename, "sshd_config.backup.") {
continue
}
// Get file info
fullPath := filepath.Join(bm.BackupDir, filename)
fileInfo, err := os.Stat(fullPath)
if err != nil {
continue
}
// Parse timestamp from filename
parts := strings.Split(filename, ".")
if len(parts) < 3 {
continue
}
timestampStr := parts[2]
createdAt, err := time.Parse("20060102_150405", timestampStr)
if err != nil {
// Try to parse as just the timestamp part
if len(parts) >= 3 {
createdAt = fileInfo.ModTime()
} else {
continue
}
}
// Determine if automatic or manual backup
isAutomatic := !strings.Contains(filename, ".manual") && len(parts) == 3
description := "Automatic backup"
if !isAutomatic {
if len(parts) > 3 {
description = strings.ReplaceAll(parts[3], "_", " ")
description = strings.Title(description)
} else {
description = "Manual backup"
}
}
backup := BackupInfo{
Path: fullPath,
Filename: filename,
CreatedAt: createdAt,
Size: fileInfo.Size(),
Description: description,
IsAutomatic: isAutomatic,
}
backups = append(backups, backup)
}
// Sort by creation time (newest first)
sort.Slice(backups, func(i, j int) bool {
return backups[i].CreatedAt.After(backups[j].CreatedAt)
})
return backups, nil
}
// RestoreBackup restores a backup file to the current SSH configuration
func (bm *BackupManager) RestoreBackup(backupPath string) error {
// Verify backup file exists
if _, err := os.Stat(backupPath); os.IsNotExist(err) {
return fmt.Errorf("backup file does not exist: %s", backupPath)
}
// Create a backup of current config before restoring
currentBackup, err := bm.CreateBackup("Before restore", true)
if err != nil {
return fmt.Errorf("failed to backup current config before restore: %v", err)
}
// Restore the backup
if err := copyFile(backupPath, bm.ConfigPath); err != nil {
return fmt.Errorf("failed to restore backup: %v", err)
}
// Test the restored configuration
cmd := exec.Command("sshd", "-t", "-f", bm.ConfigPath)
if err := cmd.Run(); err != nil {
// Configuration is invalid, restore the current backup
if restoreErr := copyFile(currentBackup.Path, bm.ConfigPath); restoreErr != nil {
return fmt.Errorf("restored config is invalid and failed to restore previous config: %v (original error: %v)", restoreErr, err)
}
return fmt.Errorf("restored configuration is invalid, reverted to previous config: %v", err)
}
return nil
}
// CleanupOldBackups removes old backup files based on retention policy
func (bm *BackupManager) CleanupOldBackups() error {
backups, err := bm.ListBackups()
if err != nil {
return fmt.Errorf("failed to list backups for cleanup: %v", err)
}
var toDelete []BackupInfo
cutoffDate := time.Now().AddDate(0, 0, -bm.RetentionDays)
// Mark old backups for deletion (keep at least the most recent ones)
for i, backup := range backups {
// Always keep the first MaxBackups
if i < bm.MaxBackups {
continue
}
// Delete if older than retention period
if backup.CreatedAt.Before(cutoffDate) {
toDelete = append(toDelete, backup)
}
}
// Delete marked backups
for _, backup := range toDelete {
if err := os.Remove(backup.Path); err != nil {
fmt.Printf("Warning: failed to delete old backup %s: %v\n", backup.Filename, err)
} else {
fmt.Printf("Deleted old backup: %s\n", backup.Filename)
}
}
return nil
}
// GetBackupStats returns statistics about backups
func (bm *BackupManager) GetBackupStats() (map[string]interface{}, error) {
backups, err := bm.ListBackups()
if err != nil {
return nil, err
}
stats := map[string]interface{}{
"total_backups": len(backups),
"automatic_count": 0,
"manual_count": 0,
"total_size_bytes": int64(0),
"oldest_backup": "",
"newest_backup": "",
}
if len(backups) == 0 {
return stats, nil
}
var totalSize int64
automaticCount := 0
manualCount := 0
for _, backup := range backups {
totalSize += backup.Size
if backup.IsAutomatic {
automaticCount++
} else {
manualCount++
}
}
stats["automatic_count"] = automaticCount
stats["manual_count"] = manualCount
stats["total_size_bytes"] = totalSize
stats["total_size_mb"] = float64(totalSize) / (1024 * 1024)