2018-04-13 18:59:03 +00:00
|
|
|
package pki
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2019-02-05 01:38:43 +00:00
|
|
|
"math"
|
2018-04-13 18:59:03 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strconv"
|
2018-12-02 01:32:33 +00:00
|
|
|
"strings"
|
2018-04-13 18:59:03 +00:00
|
|
|
)
|
|
|
|
|
2018-12-02 01:32:33 +00:00
|
|
|
const SymlinkPrefix = "_AUTH_ID_"
|
|
|
|
|
2018-04-13 18:59:03 +00:00
|
|
|
func GetCertificateAssociation(serial uint64) string {
|
2022-02-03 09:16:52 +00:00
|
|
|
return fmt.Sprintf(SymlinkPrefix+"%0[2]*[1]X", serial, int(math.Ceil(math.Log2(float64(serial))/8)*2))
|
2018-04-13 18:59:03 +00:00
|
|
|
}
|
|
|
|
|
2020-01-29 10:35:01 +00:00
|
|
|
func GetAssociation(dirname string) (assocs string, err error) {
|
|
|
|
return os.Readlink(dirname)
|
|
|
|
}
|
|
|
|
|
2018-04-13 18:59:03 +00:00
|
|
|
func GetAssociations(dirname string) (assocs []string, err error) {
|
2020-01-18 22:45:28 +00:00
|
|
|
if ds, errr := ioutil.ReadDir(dirname); errr != nil {
|
2018-04-13 18:59:03 +00:00
|
|
|
return nil, errr
|
|
|
|
} else {
|
|
|
|
for _, d := range ds {
|
2022-02-03 09:16:52 +00:00
|
|
|
if d.Mode()&os.ModeSymlink == os.ModeSymlink {
|
2018-04-13 18:59:03 +00:00
|
|
|
assocs = append(assocs, d.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetTeamSerials(dirname string, id_team int64) (serials []uint64, err error) {
|
|
|
|
// As futher comparaisons will be made with strings, convert it only one time
|
|
|
|
str_tid := fmt.Sprintf("%d", id_team)
|
|
|
|
|
|
|
|
var assocs []string
|
|
|
|
if assocs, err = GetAssociations(dirname); err != nil {
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
for _, assoc := range assocs {
|
|
|
|
var tid string
|
2018-12-02 01:32:33 +00:00
|
|
|
if tid, err = os.Readlink(path.Join(dirname, assoc)); err == nil && tid == str_tid && strings.HasPrefix(assoc, SymlinkPrefix) {
|
2018-04-13 18:59:03 +00:00
|
|
|
if serial, err := strconv.ParseUint(assoc[9:], 16, 64); err == nil {
|
|
|
|
serials = append(serials, serial)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2019-02-04 18:59:29 +00:00
|
|
|
|
|
|
|
func GetTeamAssociations(dirname string, id_team int64) (teamAssocs []string, err error) {
|
|
|
|
// As futher comparaisons will be made with strings, convert it only one time
|
|
|
|
str_tid := fmt.Sprintf("%d", id_team)
|
|
|
|
|
|
|
|
var assocs []string
|
|
|
|
if assocs, err = GetAssociations(dirname); err != nil {
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
for _, assoc := range assocs {
|
|
|
|
var tid string
|
|
|
|
if tid, err = os.Readlink(path.Join(dirname, assoc)); err == nil && tid == str_tid && !strings.HasPrefix(assoc, SymlinkPrefix) {
|
|
|
|
teamAssocs = append(teamAssocs, assoc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2022-02-03 09:16:52 +00:00
|
|
|
|
|
|
|
func DeleteTeamAssociation(dirname string, assoc string) error {
|
|
|
|
if err := os.Remove(path.Join(dirname, assoc)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|