X-Git-Url: http://www.git.cypherpunks.ru/?a=blobdiff_plain;f=passwd.go;h=95566e15da21b5fe708417c041f96babe404c51b;hb=d4dc050f024708f91e1c9ebb63595a86efda24f9;hp=dae17a5364b687107f8590caac1231bae71198b3;hpb=0d804c602fbc55f58d04fa9657950f3a6aac6d81;p=gocheese.git diff --git a/passwd.go b/passwd.go index dae17a5..95566e1 100644 --- a/passwd.go +++ b/passwd.go @@ -1,32 +1,34 @@ -/* -GoCheese -- Python private package repository and caching proxy -Copyright (C) 2019-2020 Sergey Matveev - 2019-2020 Elena Balakhonova - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, version 3 of the License. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see . -*/ +// GoCheese -- Python private package repository and caching proxy +// Copyright (C) 2019-2024 Sergey Matveev +// 2019-2024 Elena Balakhonova +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, version 3 of the License. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . 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 +53,51 @@ 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() + } + return isGood +} + +func passwdLister(fd *os.File) { + PasswordsM.RLock() + logins := make([]string, 0, len(Passwords)) + for login := range Passwords { + logins = append(logins, login) + } + PasswordsM.RUnlock() + for _, login := range logins { + fd.WriteString(login + "\n") } - passwords = passwordsNew }