(req dto.SSHConfUpdate)
| 785 | } |
| 786 | |
| 787 | func (u *SSHService) UpdateByFile(req dto.SSHConfUpdate) error { |
| 788 | var fileName string |
| 789 | switch req.Key { |
| 790 | case "authKeys": |
| 791 | currentUser, err := user.Current() |
| 792 | if err != nil { |
| 793 | return fmt.Errorf("load current user failed, err: %v", err) |
| 794 | } |
| 795 | fileName = currentUser.HomeDir + "/.ssh/authorized_keys" |
| 796 | case "sshdConf": |
| 797 | fileName = "/etc/ssh/sshd_config" |
| 798 | case "sshdConfPath": |
| 799 | fileName = req.Path |
| 800 | if !isSSHConfigPathAllowed(fileName) { |
| 801 | return buserr.WithName("ErrNotSupportType", req.Key) |
| 802 | } |
| 803 | default: |
| 804 | return buserr.WithName("ErrNotSupportType", req.Key) |
| 805 | } |
| 806 | file, err := os.OpenFile(fileName, os.O_WRONLY|os.O_TRUNC, constant.FilePerm) |
| 807 | if err != nil { |
| 808 | return err |
| 809 | } |
| 810 | defer file.Close() |
| 811 | if _, err = file.WriteString(req.Value); err != nil { |
| 812 | return err |
| 813 | } |
| 814 | if req.Key == "authKeys" { |
| 815 | return nil |
| 816 | } |
| 817 | serviceName, err := loadServiceName() |
| 818 | if err != nil { |
| 819 | return err |
| 820 | } |
| 821 | return restartSSHService(serviceName) |
| 822 | } |
| 823 | |
| 824 | func sortFileList(fileNames []sshFileItem) []sshFileItem { |
| 825 | if len(fileNames) < 2 { |
nothing calls this directly
no test coverage detected