]> Cypherpunks.ru repositories - gocheese.git/blobdiff - passwd.go
FIFO-based password management
[gocheese.git] / passwd.go
index f2ff9a472193b1b642eb5d1e8c010acea2217fc7..1a32f70269b16f2d2de2b103b7b542025387144a 100644 (file)
--- a/passwd.go
+++ b/passwd.go
@@ -19,14 +19,18 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 package main
 
 import (
+       "bufio"
        "errors"
-       "io/ioutil"
        "log"
        "os"
        "strings"
+       "sync"
 )
 
-var passwords map[string]Auther = make(map[string]Auther)
+var (
+       Passwords  map[string]Auther = make(map[string]Auther)
+       PasswordsM sync.RWMutex
+)
 
 type Auther interface {
        Auth(password string) bool
@@ -51,33 +55,39 @@ func strToAuther(verifier string) (string, Auther, error) {
        return algorithm, auther, err
 }
 
-func refreshPasswd() {
-       passwd, err := ioutil.ReadFile(*passwdPath)
-       if os.IsNotExist(err) {
-               return
-       }
-       if err != nil {
-               log.Fatal(err)
-       }
-       passwordsNew := make(map[string]Auther)
-       for i, credentials := range strings.Split(strings.TrimRight(string(passwd), "\n"), "\n") {
-               if len(credentials) == 0 || strings.HasPrefix(credentials, "#") {
+func passwdReader(fd *os.File) bool {
+       isGood := true
+       scanner := bufio.NewScanner(fd)
+       for scanner.Scan() {
+               t := scanner.Text()
+               if len(t) == 0 {
                        continue
                }
-               splitted := strings.Split(credentials, ":")
+               splitted := strings.Split(t, ":")
                if len(splitted) != 2 {
-                       log.Fatalf("%s:%d: Wrong login:password format", *passwdPath, i)
+                       log.Println("wrong login:password format:", t)
+                       isGood = false
+                       continue
                }
                login := splitted[0]
-               if _, exists := passwordsNew[login]; exists {
-                       log.Fatalf("%s:%d: %s: already exists", *passwdPath, i, login)
+               passwd := splitted[1]
+               if passwd == "" {
+                       log.Println("deleting login:", login)
+                       PasswordsM.Lock()
+                       delete(Passwords, login)
+                       PasswordsM.Unlock()
+                       continue
                }
-               _, auther, err := strToAuther(splitted[1])
+               _, auther, err := strToAuther(passwd)
                if err != nil {
-                       log.Fatalf("%s:%d: %s: %s", *passwdPath, i, login, err)
+                       log.Println("login:", login, "invalid password:", err)
+                       isGood = false
+                       continue
                }
-               passwordsNew[login] = auther
-               log.Println("added password for:", login)
+               log.Println("adding password for:", login)
+               PasswordsM.Lock()
+               Passwords[login] = auther
+               PasswordsM.Unlock()
        }
-       passwords = passwordsNew
+       return isGood
 }