github.com/tommi2day/gomodules/pwlib@v0.0.0-20230217211148-82cdbcf0a79d/get_password.go (about) 1 package pwlib 2 3 import ( 4 "errors" 5 "fmt" 6 "github.com/tommi2day/gomodules/common" 7 "os" 8 "strings" 9 10 log "github.com/sirupsen/logrus" 11 ) 12 13 // DecryptFile decripts an rsa protected file 14 func DecryptFile() (lines []string, err error) { 15 cryptedfile := PwConfig.CryptedFile 16 privatekeyfile := PwConfig.PrivateKeyFile 17 keypass := PwConfig.KeyPass 18 sessionpassfile := PwConfig.SessionPassFile 19 passflag := "open" 20 content := "" 21 if len(keypass) > 0 { 22 passflag = fmt.Sprintf("Encrypted:%s", keypass) 23 } 24 method := PwConfig.Method 25 switch method { 26 case typeOpenssl: 27 content, err = PrivateDecryptFileSSL(cryptedfile, privatekeyfile, keypass, sessionpassfile) 28 case typeGO: 29 content, err = PrivateDecryptFileGo(cryptedfile, privatekeyfile, keypass) 30 default: 31 log.Fatalf("encryption method %s not known", method) 32 os.Exit(1) 33 } 34 log.Debugf("Load data from %s with key %s(%s)", cryptedfile, privatekeyfile, passflag) 35 if err != nil { 36 log.Debug("load data failed") 37 return 38 } 39 content = strings.ReplaceAll(content, "\r", "") 40 lines = strings.Split(content, "\n") 41 log.Debug("load data success") 42 return 43 } 44 45 // EncryptFile encrypt plain text to rsa protected file 46 func EncryptFile() (err error) { 47 cryptedFile := PwConfig.CryptedFile 48 pubKeyFile := PwConfig.PubKeyFile 49 plaintextfile := PwConfig.PlainTextFile 50 sessionpassfile := PwConfig.SessionPassFile 51 method := PwConfig.Method 52 switch method { 53 case typeOpenssl: 54 err = PubEncryptFileSSL(plaintextfile, cryptedFile, pubKeyFile, sessionpassfile) 55 case typeGO: 56 err = PubEncryptFileGo(plaintextfile, cryptedFile, pubKeyFile) 57 default: 58 log.Fatalf("encryption method %s not known", method) 59 os.Exit(1) 60 } 61 log.Debugf("Encrypt data from %s with key %s into %s", plaintextfile, pubKeyFile, cryptedFile) 62 if err != nil { 63 log.Debug("encryption data failed") 64 return 65 } 66 log.Debug("encrytion data success") 67 return 68 } 69 70 // ListPasswords printout list of pwcli 71 func ListPasswords() (lines []string, err error) { 72 log.Debugf("ListPasswords entered") 73 lines, err = DecryptFile() 74 if err != nil { 75 log.Errorf("Decode Failed") 76 return 77 } 78 return 79 } 80 81 // GetPassword ask System for data 82 func GetPassword(system string, account string) (password string, err error) { 83 var lines []string 84 log.Debugf("GetPassword for '%s'@'%s' entered", account, system) 85 lines, err = DecryptFile() 86 if err != nil { 87 return 88 } 89 found := false 90 direct := false 91 for _, line := range lines { 92 if common.CheckSkip(line) { 93 continue 94 } 95 fields := strings.SplitN(line, ":", 3) 96 if len(fields) != 3 { 97 log.Debugf("Skip incomplete record %s", line) 98 continue 99 } 100 if system == fields[0] && account == fields[1] { 101 log.Debug("Found direct match") 102 if found { 103 log.Debug("Overwrite previous default candidate") 104 } 105 found = true 106 direct = true 107 password = fields[2] 108 break 109 } 110 if fields[0] == "!default" && account == fields[1] { 111 password = fields[2] 112 log.Debug("found new default match candidate") 113 if found { 114 log.Debug("Overwrite previous default candidate") 115 } 116 found = true 117 } 118 } 119 // not found 120 if !found { 121 msg := fmt.Sprintf("no record found for '%s'@'%s'", account, system) 122 log.Debug("GetPassword finished with no Match") 123 err = errors.New(msg) 124 return 125 } 126 127 // found 128 if !direct { 129 log.Debug("use default entry") 130 } 131 return 132 }